-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcs_reader.py
50 lines (35 loc) · 1.75 KB
/
mcs_reader.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
import xml.etree.ElementTree as ET
class MSCReader:
def __init__(self, filepath):
self.filepath = filepath
self.tree = ET.parse(self.filepath)
self.root = self.tree.getroot()
self.ns = '{http://www.icpsr.umich.edu/DDI}'
def __enter__(self):
return self
def readInstance(self):
# print self.tree.find('.')[3]
#print self.tree.find('.//' + self.ns + 'stdyDscr//' + self.ns + 'titl').text
#print self.tree.find('.//' + self.ns + 'dataDscr/' + self.ns + 'varGrp/').text
#print self.tree.find('.//' + self.ns + 'dataDscr/' + self.ns + 'varGrp').get('ID')
#print self.tree.find('.//' + self.ns + 'dataDscr/'+ self.ns + 'var').get('ID')
#print self.tree.find('.//' + self.ns + 'dataDscr/'+ self.ns + 'var').get('name')
#print self.tree.find('.//' + self.ns + 'dataDscr//'+ self.ns + 'var/' + self.ns + 'labl').text
#print self.tree.find('.//' + self.ns + 'dataDscr//'+ self.ns + 'var//' + self.ns + 'qstnLit').text
#print self.root[0][0]
#print self.root.attrib
for v in self.root.findall('.//' + self.ns + 'dataDscr//'+ self.ns + 'var'):
question_name = ''
question_label = ''
question_text = ''
question_ID = v.get('ID')
if v.get('name') != None:
question_name = v.get('name')
if v.find('./' + self.ns + 'labl') != None:
question_label = v.find('./' + self.ns + 'labl').text
if v.find('.//' + self.ns + 'qstnLit') != None:
question_text = v.find('.//' + self.ns + 'qstnLit').text
print question_ID
print question_label
print question_name
print question_text