Reputation: 47
This is follow up to this question - 2d list in python
The answer by @Kroolik addresses my issue, but I'm stuck on another thing
consider my files is as follows
junk....
junk....
junk
required....
junk...
required....
junk...
when i read thro csv.dictreader, how do I skip the junk lines? also, I only know the first and last 'required' and the 'junk' in between. The initial 'junk' can be anything and any number of lines.
I tried the below
version_new = open(file_version_new, 'r')
flag = 0
for row in version_new:
if "JID" in row:
flag = 1 #starting of the 'required section
if "Total text" in row:
flag = 2 #end of required section
if flag == 1:
list_top_version_new.append(row.split())
if flag == 2:
#do something
reader = csv.DictReader(list_top_version_new)
for line in reader:
print(line)
but this doesnt seem to work. Any help would be appreciated. thanks
Upvotes: 0
Views: 1074
Reputation: 1123820
You can loop within the loop, getting the next lines until you are at the end:
for row in version_new:
if "JID" in row:
# in required section, loop until end:
for row in version_new:
if "Total text" in row:
break
list_top_version_new.append(row)
# Anything outside of the required section is ignored.
Note that row.split()
isn't needed; csv.DictReader
gives you a dictionary object, with the row already split out into values already.
list_top_version_new
is also a list of dictionaries, no need to put those through csv.DictReader()
again. And since you are already looping over that section of your input file, why not just directly in that loop do your work? So, instead of a separate loop over list_top_version_new
at the end, replace list_top_version_new.append(row)
with whatever work you need to do with the row:
for row in version_new:
if "JID" in row:
# in required section, loop until end:
for row in version_new:
if "Total text" in row:
break
print(row)
Upvotes: 1