简体   繁体   English

在Python中读取.csv而不循环遍历整个文件?

[英]Reading .csv in Python without looping through the whole file?

The only way I've seen Python's csv.reader used is in a for loop, which goes through the whole file without saving past values of the read in variables. 我看过使用Python的csv.reader的唯一方法是在for循环中,它遍历整个文件而不保存读取变量的过去值。 I only need to work with 2 consecutive lines of the (enormous) file at a time. 我只需要一次处理(巨大的)文件的2个连续行。 Using the csv.reader for loop, I only have 1 line at a time. 使用csv.reader for循环,我一次只有1行。

Is there a way to use Python's csv module for taking in only one line of a csv file without having to finish reading the file to the end? 有没有办法使用Python的csv模块只接收一行csv文件而不必完成文件的读取?

I need to set variables to the values in the first line, set a second set of variables to the values of the next line, use the two sets of variables simultaneously for computations, then overwrite the first set of variables with the second set, and read a new line to overwrite the second set. 我需要将变量设置为第一行中的值,将第二组变量设置为下一行的值,同时使用两组变量进行计算,然后使用第二组覆盖第一组变量,并且读取新行以覆盖第二组。

There's nothing forcing you to use the reader in a loop. 没有什么可以强迫你在循环中使用阅读器。 Just read the first line, then read the second line. 只需阅读第一行,然后阅读第二行。

import csv
rdr = csv.reader(open("data.csv"))
line1 = rdr.next() # in Python 2, or next(rdr) in Python 3
line2 = rdr.next()

Read CSV: 阅读CSV:

readCSV = csv.reader(csvFile, delimiter=',')

Read the next row in Python 2.7: 阅读Python 2.7中的下一行:

    row = readCSV.next()

Read the next row in Python 3.4: 阅读Python 3.4中的下一行:

    row = readCSV.__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: 来自itertools模块:

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

The obvious answer seems to be to just store the previous line on each iteration. 显而易见的答案似乎是在每次迭代时只存储前一行。

>>> for x in csv.DictReader(stream):
...   print prevLine
...   print x
...   prevLine = x
....

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? 从传统知识中肆无忌惮地窃取......主要是剩下的问题是,OP想要对文件的第一行和最后一行做什么?

prevLine = None

for x in csv.DictReader(stream):
   if prevLine is not None:
       DoWork(prevLine, x)
   else:
       Initialize(x)
   prevLine = x

Finalize(prevLine)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM