Reading A File For A Certain Section In Python
I am trying to follow a answer given here: How to only read lines in a text file after a certain string using python? in reading only the line after a certain phrase in which I wen
Solution 1:
You have to skip the rest of the for loop after detecting the "header". In your code, you're setting found_type
to True
and then the if found_type:
check matches.
found_type = False
t_ype = []
withopen('test.xml', 'r') as f:
for line in f:
if'<type>'in line:
found_type = Truecontinue# This is the only change to your code.# When the header is found, immediately go to the next lineif found_type:
if'</type>'in line:
found_type = Falseelse:
t_line = str(line).rstrip('\n')
t_ype.append(t_line)
Solution 2:
The simplest approach is a double loop with yield:
defsection(fle, begin, end):
with open(fle) as f:for line inf:# found start of section so start iterating from next lineif line.startswith(begin):
for line inf:# found end so end functionif line.startswith(end):
return# yield every line in the sectionyield line.rstrip()
Then just either call list(section('test.xml','<type>','</type>'))
or iterate over for line in section('test.xml','<type>','</type>'):use lines
,if you have repeating sections then swap the return for a break. You also don't need to call str on the lines as they are already strings, if you have a large file then the groupby approach in the comments might be a better alternative.
Post a Comment for "Reading A File For A Certain Section In Python"