forked from MITLibraries/archivesspace-api-python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eadToCsv.py
182 lines (175 loc) · 6.83 KB
/
eadToCsv.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import csv
import lxml
from bs4 import BeautifulSoup
def extractValuesFromcmpntLvl(cmpntLvl):
"""Extract value from the specified component level of the record."""
level = cmpntLvl.name
cmpntLvlLabel = cmpntLvl['level']
unittitle = cmpntLvl.find('did').find('unittitle').text
unittitle = unittitle.replace('\n', '')
controlAccess = []
originationList = []
try:
physdesc = cmpntLvl.find('did').find('physdesc').text
physdesc = physdesc.replace('\n', '')
except:
physdesc = ''
try:
unitdate = cmpntLvl.find('did').find('unitdate')
dateExpression = unitdate.text.replace('\n', '')
try:
dateType = unitdate['entity_type']
except:
dateType = ''
try:
dateNormal = unitdate['normal']
beginDate = dateNormal[:dateNormal.index('/')]
endDate = dateNormal[dateNormal.index('/') + 1:]
except:
beginDate = ''
endDate = ''
except:
dateExpression = ''
dateType = ''
beginDate = ''
endDate = ''
try:
scopecontentElement = cmpntLvl.find('scopecontent')
scopecontentElement = scopecontentElement.find_all('p')
scopecontent = ''
for paragraph in scopecontentElement:
paragraphText = paragraph.text.replace('\n', '')
scopecontent = scopecontent + paragraphText
except:
scopecontent = ''
try:
subjects = cmpntLvl.find('controlaccess').find_all()
for subject in subjects:
controlAccess.append(subject.text)
except:
subjects = ''
try:
container1 = cmpntLvl.find('did')
container1 = container1.find_all('container')[0].text
except:
container1 = ''
try:
containerId1 = cmpntLvl.find('did')
containerId1 = containerId1.find_all('container')[0]['a_id']
except:
containerId1 = ''
try:
containerType1 = cmpntLvl.find('did')
containerType1 = containerType1.find_all('container')[0]['label']
except:
containerType1 = ''
try:
container2 = cmpntLvl.find('did')
container2 = container2.find_all('container')[1].text
except:
container2 = ''
try:
containerId2 = cmpntLvl.find('did')
containerId2 = containerId2.find_all('container')[1]['a_id']
except:
containerId2 = ''
try:
containerType2 = cmpntLvl.find('did')
containerType2 = containerType2.find_all('container')[1]['label']
except:
containerType2 = ''
try:
originations = cmpntLvl.find('did').find_all('origination')
for origination in originations:
if origination.find()['role'] == 'spn':
originationList.append(origination.text)
except:
originationList = ''
global sortOrder
sortOrder += 1
f.writerow([sortOrder] + [level] + [cmpntLvlLabel]
+ [containerType1] + [container1] + [containerType2]
+ [container2] + [unittitle] + [physdesc] + [dateExpression]
+ [dateType] + [beginDate] + [endDate] + [scopecontent]
+ [controlAccess] + [originationList] + [containerId1]
+ [containerId2])
filepath = ''
fileName = ''
xml = open(filepath + fileName)
f = csv.writer(open(filepath + 'eadFields.csv', 'w', newline=''))
f.writerow(['sortOrder'] + ['hierarchy'] + ['level'] + ['containerType1']
+ ['container1'] + ['containerType2'] + ['container2']
+ ['unittitle'] + ['physdesc'] + ['dateexpression'] + ['datetype']
+ ['begindate'] + ['enddate'] + ['scopecontent'] + ['controlAccess']
+ ['origination'] + ['containerId1'] + ['containerId2'])
uppercmpntLvls = BeautifulSoup(xml, 'lxml').find('dsc').find_all('c01')
sortOrder = 0
for uppercmpntLvl in uppercmpntLvls:
cmpntLvlLabel = uppercmpntLvl['level']
unittitle = uppercmpntLvl.find('did').find('unittitle')
unittitle = unittitle.text.replace('\n', '')
try:
physdesc = uppercmpntLvl.find('did').find('physdesc').text
physdesc = physdesc.replace('\n', '')
except:
physdesc = ''
try:
unitdate = uppercmpntLvl.find('did').find('unitdate')
dateExpression = unitdate.text.replace('\n', '')
try:
dateType = unitdate['entity_type']
except:
dateType = ''
try:
dateNormal = unitdate['normal']
beginDate = dateNormal[:dateNormal.index('/')]
endDate = dateNormal[dateNormal.index('/') + 1:]
except:
beginDate = ''
endDate = ''
except:
dateExpression = ''
dateType = ''
beginDate = ''
endDate = ''
try:
scopecontentElement = uppercmpntLvl.find('scopecontent')
scopecontentElement = scopecontentElement.find_all('p')
scopecontent = ''
for paragraph in scopecontentElement:
paragraphText = paragraph.text.replace('\\n', '')
scopecontent = scopecontent + paragraphText
except:
scopecontent = ''
sortOrder += 1
f.writerow([sortOrder] + ['c01'] + [cmpntLvlLabel] + [''] + ['']
+ [''] + [''] + [unittitle] + [physdesc] + [dateExpression]
+ [dateType] + [beginDate] + [endDate] + [scopecontent] + ['']
+ [''] + [''] + [''])
cmpntLvlArray = uppercmpntLvl.find_all('c02')
for cmpntLvl in cmpntLvlArray:
extractValuesFromcmpntLvl(cmpntLvl)
cmpntLvlArray = cmpntLvl.find_all('c03')
for cmpntLvl in cmpntLvlArray:
extractValuesFromcmpntLvl(cmpntLvl)
cmpntLvlArray = cmpntLvl.find_all('c04')
for cmpntLvl in cmpntLvlArray:
extractValuesFromcmpntLvl(cmpntLvl)
cmpntLvlArray = cmpntLvl.find_all('c05')
for cmpntLvl in cmpntLvlArray:
extractValuesFromcmpntLvl(cmpntLvl)
cmpntLvlArray = cmpntLvl.find_all('c06')
for cmpntLvl in cmpntLvlArray:
extractValuesFromcmpntLvl(cmpntLvl)
cmpntLvlArray = cmpntLvl.find_all('c07')
for cmpntLvl in cmpntLvlArray:
extractValuesFromcmpntLvl(cmpntLvl)
cmpntLvlArray = cmpntLvl.find_all('c08')
for cmpntLvl in cmpntLvlArray:
extractValuesFromcmpntLvl(cmpntLvl)
cmpntLvlArray = cmpntLvl.find_all('c09')
for cmpntLvl in cmpntLvlArray:
extractValuesFromcmpntLvl(cmpntLvl)
cmpntLvlArray = cmpntLvl.find_all('c10')
for cmpntLvl in cmpntLvlArray:
extractValuesFromcmpntLvl(cmpntLvl)