-
Notifications
You must be signed in to change notification settings - Fork 1
/
RIS_selectDATAbyKEYWORD.py
61 lines (52 loc) · 2 KB
/
RIS_selectDATAbyKEYWORD.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
# script to read only data selected by keyword from RIS files
# results are written to CSV
# please note that rispy is the follow-up version of RISparser
# download and documentation: https://pypi.org/project/rispy/
import os
from pprint import pprint
import rispy
# define file paths for local RIS files and final output
filepath='C:\\XXXXX'
outpath='C:\\XXXXX'
# define keyword and output lists
keyword="yourstring"
titles=[]
wrong_ids=[]
# define filepath as directory containing iterable files and read each file
for f in os.listdir(filepath):
#print(f) # return file names, e.g. Primo_RIS_Export.ris
f_path=os.path.join(filepath, f)
with open(f_path, 'r', encoding="latin-1") as bibliography_file:
#print(bibliography_file) # returns RIS meta-information for file
try:
data=rispy.load(bibliography_file, strict=False) # accepts non-standard RIS if "strict=False"
finddata(data) # get data via function
except:
OSError
continue
# entries are called based on standard RIS format
# for files deviating from this format, you may need to use a tag-key-mapper
# check rispy documentation for further details
def finddata(x):
try:
title=x[0]['primary_title'] # get first and only item from list and dictionary data by key
print(title)
if keyword in title:
titles.append(title)
else:
wrong_id=x[0]['id'] # get first and only item from list and dictionary data by key
wrong_ids.append(wrong_id)
except:
KeyError
print("\n This item has no title: ", x[0]['id'])
def exportfile(outpath): # export data to csv
out=open(os.path.join(outpath, 'ris_out.csv'), 'w', encoding="utf_8_sig") # force BOM to correctly display special characters in EXCEL
for t in titles:
out.write(t)
out.write('\n\n')
out.close()
# show or export results
print(titles)
print(wrong_ids)
exportfile(outpath)
print("done")