在Python中反复读取CSV文件? [英] Reading from CSVs in Python repeatedly?

查看:891
本文介绍了在Python中反复读取CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检查提取的数据的值对我已经有一个csv。它只会循环遍历CSV的行一次,我只能检查feed.items()的一个值。有没有值我需要重置某处?有没有更好的/更有效的方法来做到这一点?感谢。

  orig = csv.reader(open(googlel.csv,rb),delimiter =';' )
goodrows = []
for feed在gotfeeds中:
为链接,feed.items中的注释():
为原始行:
打印链接
if link [1]中的链接:
row.append(comments)
goodrows.append(row)


解决方案

您可以通过重置文件对象的读取位置来重置CSV迭代器。

  data = open(googlel.csv,rb)
orig = csv.reader(data,delimiter =';')
goodrows = ]
for feed in gotfeeds:
for link,feed.items()中的注释:
data.seek(0)
for row in orig:
打印链接
如果在行[1]中的链接:
row.append(comments)
goodrows.append(row)

I'm trying to check the value of extracted data against a csv I already have. It will only loop through the rows of the CSV once, I can only check one value of feed.items(). Is there a value I need to reset somewhere? Is there a better/more efficient way to do this? Thanks.

orig = csv.reader(open("googlel.csv", "rb"), delimiter = ';')
goodrows = []
for feed in gotfeeds:    
   for link,comments in feed.items():
       for row in orig:
           print link
           if link in row[1]:
               row.append(comments)
               goodrows.append(row)

解决方案

You can "reset" the CSV iterator by resetting the read position of the file object.

data = open("googlel.csv", "rb")
orig = csv.reader(data, delimiter = ';')
goodrows = []
for feed in gotfeeds:    
   for link,comments in feed.items():
       data.seek(0)
       for row in orig:
           print link
           if link in row[1]:
               row.append(comments)
               goodrows.append(row)

这篇关于在Python中反复读取CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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