-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsv4saf.py
executable file
·58 lines (53 loc) · 1.74 KB
/
tsv4saf.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
#! /usr/bin/env python3
import sys
import csv
import time
import doilookup
# Check for the input file
if len(sys.argv) < 3:
print()
print('Usage: ./tsv4saf.py [wos_tsv_file] [output_tsv_file]')
print('Or: python3 tsv4saf.py [wos_tsv_file] [output_tsv_file]')
print()
sys.exit(1)
else:
infile = sys.argv[1]
outfile = sys.argv[2]
# read in the file converting rows into dictionaries
rows = []
with open(infile, 'rt', encoding='utf-8-sig') as intsv:
rdr = csv.DictReader(intsv, restkey='extra_columns', delimiter='\t')
for r in rdr:
rows.append(r)
newrows = []
for row in rows:
time.sleep(0.1)
rowaug = doilookup.lookup(row['DI'])
newrow = row.copy()
newrow.update(rowaug)
newrows.append(newrow)
fields = ['AF', 'TI', 'DE', 'ID', 'AB', 'pb', 'ty', 'id', 'fu', 'li', 'issn',
'ct', 'af', 'issued', 'bc']
mapping = {'AF': 'dc.contributor.author',
'TI': 'dc.title[en]',
'DE': 'dc.subject[en]',
'ID': 'dc.subject2',
'AB': 'dc.description.abstract[en]',
'pb': 'dc.publisher[en]',
'ty': 'dc.type[en]',
'id': 'dc.identifier.uri',
'fu': 'dc.description.sponsorship[en]',
'li': 'dc.rights.uri',
'issn': 'ISSN',
'ct': 'Journal_Name',
'af': 'uws.contributor.affiliation2[en]',
'issued': 'dc.date.issued',
'bc': 'dcterms.bibliographicCitation[en]',
}
newfieldnames = [ mapping[k] for k in fields ]
with open(outfile, 'wt', encoding='utf-8', newline='') as outf:
w = csv.DictWriter(outf, fieldnames=newfieldnames,
delimiter='\t')
w.writeheader()
for row in newrows:
w.writerow({mapping[k]: row[k] for k in fields})