如何将 CSV 文件中的一行与上一行进行比较?
我的 if 条件需要它:
if row[0] != row[0][of previous row]:
do something....
请您参考如下方法:
这个程序做你想做的事:
import csv
f = open("some.csv", "r+")
ff = csv.reader(f)
pre_line = ff.next()
while(True):
try:
cur_line = ff.next()
if pre_line == cur_line:
pass #replace pass with do_something()
pre_line = cur_line
except:
break
正如您在上面看到的,我遍历了 csv 文件中的行,并使用之前的 using 和迭代器检查了每一行。如果它们相等,我调用 do_something() 方法,如果它们不相等,它就转到下一行。
