Reading from CSVs in Python repeatedly?
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", "开发者_Python百科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)
Making orig
a list avoids the need to reset/reparse the csv:
orig = list(csv.reader(open("googlel.csv", "rb"), delimiter = ';'))
精彩评论