我看到使用 Python 的 csv.reader 的唯一方法是在 for 循环中,它遍历整个文件而不保存变量中读取的过去值。我一次只需要处理 2 行(巨大的)文件。使用 csv.reader for 循环,我一次只有 1 行。
有没有办法使用 Python 的 csv 模块只接收 csv 文件的一行,而不必将文件读完?
我需要将变量设置为第一行中的值,将第二组变量设置为下一行的值,同时使用两组变量进行计算,然后用第二组变量覆盖第一组变量,然后读取新行以覆盖第二组。
没有什么强迫您循环使用阅读器。只需阅读第一行,然后阅读第二行。
import csv
rdr = csv.reader(open("data.csv"))
line1 = rdr.next() # in Python 2, or next(rdr) in Python 3
line2 = rdr.next()
If you're always looking at exactly two consecutive lines, it sounds to me like you might benefit from using the pairwise recipe. From the itertools module:
from itertools import tee, izip
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
You would use this like so:
for first_dict, second_dict in pairwise(csv.DictReader(stream)):
# do stuff with first_dict and second_dict
读取 CSV:
readCSV = csv.reader(csvFile, delimiter=',')
阅读 Python 2.7 中的下一行:
row = readCSV.next()
阅读 Python 3.4 中的下一行:
row = readCSV.__next__()
Blatant stealing from TK... ...mostly the question that remains is, what does the OP want to do with the first and last lines of the file?
prevLine = None
for x in csv.DictReader(stream):
if prevLine is not None:
DoWork(prevLine, x)
else:
Initialize(x)
prevLine = x
Finalize(prevLine)
显而易见的答案似乎是在每次迭代中只存储前一行。
>>> for x in csv.DictReader(stream):
... print prevLine
... print x
... prevLine = x
....