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

发布于 2024-09-06 10:35:32 字数 253 浏览 6 评论 0原文

我见过Python的csv.reader使用的唯一方法是在for循环中,它遍历整个文件而不保存变量中读取的过去的值。我一次只需要处理(巨大)文件的连续两行。使用 csv.reader for 循环,我一次只有 1 行。

有没有一种方法可以使用Python的csv模块只读取csv文件的一行,而不必读完文件到最后?

我需要将变量设置为第一行中的值,将第二组变量设置为下一行的值,同时使用两组变量进行计算,然后用第二组变量覆盖第一组变量,然后读取新行以覆盖第二组。

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. I only need to work with 2 consecutive lines of the (enormous) file at a time. Using the csv.reader for loop, I only have 1 line at a time.

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆梦 2024-09-13 10:35:32

没有什么强迫您循环使用阅读器。只需阅读第一行,然后阅读第二行。

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

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()
一念一轮回 2024-09-13 10:35:32

如果您总是查看连续的两行,那么在我看来,您可能会受益于使用成对 食谱。从 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)

您可以像这样使用它:

for first_dict, second_dict in pairwise(csv.DictReader(stream)):
    # do stuff with first_dict and second_dict

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
演出会有结束 2024-09-13 10:35:32

读取 CSV:

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

在 Python 2.7 中读取下一行:

    row = readCSV.next()

在 Python 3.4 中读取下一行:

    row = readCSV.__next__()

Read CSV:

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

Read the next row in Python 2.7:

    row = readCSV.next()

Read the next row in Python 3.4:

    row = readCSV.__next__()
公布 2024-09-13 10:35:32

明显的答案似乎是在每次迭代时只存储前一行。

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

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
....
回心转意 2024-09-13 10:35:32

公然窃取 TK... ...剩下的主要问题是,OP 想要对文件的第一行和最后一行做什么?

prevLine = None

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

Finalize(prevLine)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文