使用csv.dictreader时跳过行 [英] python - skipping lines while using csv.dictreader

查看:44
本文介绍了使用csv.dictreader时跳过行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此问题的后续内容- Python中的2d列表

This is follow up to this question - 2d list in python

@Kroolik的回答解决了我的问题,但我陷入了另一件事

The answer by @Kroolik addresses my issue, but I'm stuck on another thing

考虑我的文件如下

junk....
junk....
junk
required....
junk...
required....
junk...

当我通过csv.dictreader阅读时,如何跳过垃圾行?另外,我只知道第一个和最后一个必需"以及中间的垃圾".最初的垃圾"可以是任何行数和任何数量的行.

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.

我尝试了以下

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

推荐答案

您可以在循环中循环,获取下一行直到最后:

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.

请注意,不需要 row.split() csv.DictReader 为您提供了一个字典对象,该行已被拆分为多个值.

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 也是词典列表,无需再次将其放入 csv.DictReader() .并且由于您已经在输入文件的该部分中进行了循环,为什么不直接在该循环中直接 做您的工作呢?因此,与其在最后单独遍历 list_top_version_new ,不如将 list_top_version_new.append(row)替换为您需要对该行进行的所有工作:

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)

这篇关于使用csv.dictreader时跳过行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆