python - 反复从 Python 中读取 CSV?

标签 python csv

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

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 迭代器。

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 - 反复从 Python 中读取 CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2868354/

相关文章:

python - 列表索引超出范围,python

JavaScript 将数据解析为对象

Python-解析结构化文本到Excel

ruby-on-rails - 生成的 CSV 中的引号

python - 使用 CGI 保存临时文件 (Ubuntu)

python - PyCrypto:仅使用文件中的公钥解密(无私钥+公钥)

java - 将行读入列表列表中

Java将CSV文件读入数组但忽略第一行

python - 为什么 pep8 建议在评论中使用两个空格?

python - 为什么从 stdin 读取一行会阻止从 Python 中的 stdin.buffer 读取