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

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

问题描述

我正在尝试根据我已有的 csv 检查提取数据的值.它只会遍历 CSV 的行一次,我只能检查 feed.items() 的一个值.是否有需要在某处重置的值?有没有更好/更有效的方法来做到这一点?谢谢.

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)

推荐答案

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

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天全站免登陆