0

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

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. If you have seen the post that I've [linked] (stackoverflow.com/questions/20036271/…), I'm using csv.Dictreader because, it makes the column names as keys automatically which is my main concern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.