-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitSubjectsByCategory.py
65 lines (55 loc) · 2.17 KB
/
splitSubjectsByCategory.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
import csv
import argparse
import re
from datetime import datetime
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', help='enter filename with csv.')
parser.add_argument('-b', '--batch', help='Batch letter to name outputs.')
args = parser.parse_args()
if args.file:
filename = args.file
else:
filename = input('Enter filename (including \'.csv\'): ')
if args.batch:
batch = args.batch
else:
batch = input('Enter batch letter: ')
dt = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
f = csv.writer(open('subjectsToCheckAgainstFASTAndMESH_Batch'+batch+'_'+dt+'.csv', 'w'))
f.writerow(['uri']+['dc.subject']+['cleanedSubject'])
f2 = csv.writer(open('listSubjectsToCheckAgainstFASTAndMESH_Batch'+batch+'_'+dt+'.csv', 'w'))
f2.writerow(['uri']+['dc.subject']+['cleanedSubject'])
row_count = 0
newrow_count = 0
expectedvalue_count = 0
with open(filename) as itemMetadataFile:
itemMetadata = csv.DictReader(itemMetadataFile)
for row in itemMetadata:
row_count = row_count + 1
uri = row['uri']
dc_subject = row['dc.subject']
newValue = row['newValue'].strip()
category = row['category'].strip()
if category == 'list':
newValue = re.sub(',$', '', newValue) # Removes comma from end of string
if newValue.count(',') > 0:
newValueList = newValue.split(',')
elif newValue.count(r'(\n|\r\n)') > 0:
newValueList = newValue.split(r'(\n|\r\n)')
else:
print("can't split!!")
print(newValue)
value_count = len(newValueList)
expectedvalue_count = expectedvalue_count + value_count
for value in newValueList:
value = value.strip()
f2.writerow([uri]+[dc_subject]+[value])
newrow_count = newrow_count + 1
elif category == 'remove':
newValue = newValue.replace('.', '')
f.writerow([uri]+[dc_subject]+[newValue])
else:
f.writerow([uri]+[dc_subject]+[newValue])
print('total_original_rows: '+str(row_count))
print('expected_count:'+str(expectedvalue_count))
print('newrow_count: '+str(newrow_count))