Reading from CSVs in Python repeatedly?
我正在尝试根据我已经拥有的csv检查提取数据的值。它只循环一次csv的行,我只能检查feed.items()的一个值。是否有需要重置的值?有没有更好/更有效的方法?谢谢。
1 2 3 4 5 6 7 8 9 | 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) |
你可以"重置"的迭代器的resetting CSV文件读取位置的对象。
1 2 3 4 5 6 7 8 9 10 11 | 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) |
制作
1 | orig = list(csv.reader(open("googlel.csv","rb"), delimiter = ';')) |