This repository has been archived by the owner on Mar 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ris.py
72 lines (58 loc) · 2.35 KB
/
ris.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pandas as pd
import logging
def ris_detect(raw):
""" Detect RIS format style. """
if raw.startswith('TY -'):
logging.debug('RIS file format detected.')
return 'ris'
elif raw.startswith('%0'):
logging.debug('Endnote file format detected.')
return 'endnote'
else:
logging.debug('RIS format not identified.')
raise Exception(f'Data scheme not recognised. Please check file format.\nBeginning of file: "{raw[:20]}"')
def ris_parse(ris_file):
""" Read RIS file an parse rows and values to list of lists. """
with open(ris_file, 'r', encoding='utf-8-sig') as f:
raw = f.read()
data_scheme = ris_detect(raw)
data = raw.strip()
entry_sep = '\n\n' # Use 'ER - ' or '\n\n' as entry separator.
line_sep = '\n'
# Split data and remove empty rows (Endnote format)
documents = [item for item in data.split(entry_sep) if item]
table = [[item for item in doc.split(line_sep)] for doc in documents]
return table, data_scheme
def ris_df(ris_file):
""" Extract and return data as DataFrame. """
table, data_scheme = ris_parse(ris_file)
# Empty template DataFrame.
df = pd.DataFrame(columns=['title', 'abstract', 'source', 'year', 'publisher', 'type'], index = range(len(table)))
# Extract relevant data from RIS file table.
if data_scheme == 'ris':
for n, j in enumerate(table):
for i in j:
if i.startswith('TI'):
df.loc[n]['title'] = i[6:]
if i.startswith('AB'):
df.loc[n]['abstract'] = i[6:]
if i.startswith('T2'):
df.loc[n]['source'] = i[6:]
if i.startswith('PY'):
df.loc[n]['year'] = i[6:]
if i.startswith('M3'):
df.loc[n]['type'] = i[6:]
else:
for n, j in enumerate(table):
for i in j:
if i.startswith('%T'):
df.loc[n]['title'] = i[3:]
if i.startswith('%X'):
df.loc[n]['abstract'] = i[3:]
if i.startswith('%B'):
df.loc[n]['source'] = i[3:]
if i.startswith('%D'):
df.loc[n]['year'] = i[3:]
if i.startswith('%0'):
df.loc[n]['type'] = i[3:]
return df