Disclaimer:
- I might be asking this question "the wrong way" - I'm still new to programming, so I might be lacking the correct terminology and understanding to ask the question properly. Sorry about that if I am.
- I know you can't parse [X]HTML with regex. I'm not parsing it. I'm just matching a string to get the count of that string's line.
- I've tried
lxml
anditer()
, as well asiterparse()
, and I've tried it this way and this way. It didn't work for me. I'm doing it this way. - I know the code here isn't efficient (specifically compiling the regex expression each time the function is called) - I've re-written it like this to provide the "minimum needed code" to ask the question, plus it makes it a little easier to understand what I'm trying to achieve I think.
With that all out the way; this code works and does what I want it to do, which is basically to take a huge set of xml records and turning it into a dictionary in the form of { record_id : {element_tag : element_val, element_tag : element_val, ...} }
.
Since the record_id is nested within the record itself, I first use the record_splitter
to identify the count of <RECORD>
and </RECORD>
(the beg
and end
). I pass beg
and end
into dic_factory
which finds these positions, does something with the elements nested within that, then sets 'beg' to end +1
, and passes it back into the record_splitter
function.
My question is whether there is a better way to implement this loop between functions, preferably under if __name__ == '__main__'
, so that I can move more of my constants (e.g. the regex statements) under that as well.
Code:
# stored as a text file, but here for clarity
list_of_lines = """
<RECORD>
<TITLE>MISS</TITLE>
<NAME>ELIZABETH</NAME>
<SURNAME>II</SURNAME>
<ADDRESS1>1 BUCKINGHAM PALACE</ADDRESS1>
<ADDRESS2>LONDON</ADDRESS2>
<ADDRESS3>GREATER LONDON</ADDRESS3>
<POST_CODE>W1 11A</POST_CODE>
<CASE_NUM>Q1QQ1234</CASE_NUM>
<ID>32145698</ID>
<LAST_UPDATE_DATE>2016-12-12</LAST_UPDATE_DATE>
</RECORD>
<RECORD>
<TITLE>MR</TITLE>
<NAME>PRINCE</NAME>
<SURNAME>PHILLIP</SURNAME>
<ADDRESS1>1 BUCKINGHAM PALACE</ADDRESS1>
<ADDRESS2>LONDON</ADDRESS2>
<ADDRESS3>GREATER LONDON</ADDRESS3>
<POST_CODE>W1 11A</POST_CODE>
<CASE_NUM>K5KK4321</CASE_NUM>
<ID>56987412</ID>
<LAST_UPDATE_DATE>2017-01-16</LAST_UPDATE_DATE>
</RECORD>
<RECORD>
"""
class recordManager:
def __init__(self):
self.r_location = "list_of_lines.txt"
def record_splitter(self, beg):
re_beg_spl = re.compile(".*<RECORD>")
re_end_spl = re.compile(".*(<\\/RECORD>)")
end = None
for count, line in enumerate( open(self.r_location) ):
if count > beg:
if re_end_spl.match(line):
end = count
if not re_end_spl.match(line):
if re_beg_spl.match(line):
beg = count
else:
break
recordManager.dic_factory(self, beg, end)
def dic_factory(self, beg, end):
re_casenum = re.compile(".*<CASE_NUM>(.*)<\\/CASE_NUM>")
re_tag_val = re.compile(".*<(\\w*)>(.*)<.*")
id_ = None
tags = []
vals = []
for count, line in enumerate( open(self.r_location) ):
if beg < count < end:
if re_casenum.match(line):
m = re_casenum.match(line)
id_ = m.group(1)
if re_tag_val.match(line):
m = re_tag_val.match(line)
tags.append( m.group(1) )
vals.append( m.group(2) )
beg = end +1
print {id_ : dict(zip(tags, vals)) }
# {32145698 : {'POST_CODE': 'W1 11A', 'SURNAME': 'II', 'NAME': 'ELIZABETH', 'TITLE': 'MISS', 'ADDRESS1': '1 BUCKINGHAM PALACE', 'ADDRESS2': 'LONDON', 'ADDRESS3': 'GREATER LONDON', 'RECORD_TYPE': '1', 'CASE_NUM': 'Q1QQ1234', 'LAST_UPDATE_DATE': '2016-12-12', 'ID': '32145698'}}
self.record_splitter(beg)
if __name__ == '__main__':
inst_fol = record_manager(file)
recordManager.record_splitter(inst_folder, 0)
My problem is that I don't know how to loop 'beg' back into record_splitter
outside of the functions / from main:
if __name__ == '__main__':
inst_fol = record_manager(file)
beg, end = recordManager.record_splitter(inst_folder, 0)
Is this "looping from within functions" and if not, what's the better approach?