【发布时间】:2016-03-24 21:39:47
【问题描述】:
我想遍历一个 csv 文档并经常检查下一行的值,但坚持使用相同的行号。 .next() 正在跳过一行,这对我不起作用,除非我能以某种方式再次返回。有没有一种简单的方法可以做到这一点?我尝试了成对/循环,但如果文件很大,则需要的时间太长。 我的示例代码是:
file1 = open("name.csv", 'rb')
reader = csv.DictReader(file1)
new_rows_list = []
for row in reader:
if row['IsFixated'] == 'TRUE':
new_row = [row['Result'], 'Fixation']
new_rows_list.append(new_row)
else:
new_row = [row['Result'], 'Saccade']
new_rows_list.append(new_row)
nextRow = reader.next() # check for next row ??
if nextRow['IsFixated'] == 'TRUE':
new_rows_list = CheckPreviousPositions(new_rows_list)
file1.close()
【问题讨论】: