为什么我只能使用一次阅读器对象?

问题描述:

我正在尝试使用csv模块将一列数字读入python.我得到以下行为:

I'm trying to read a column of numbers into python with the csv module. I get the following behavior:

import csv

f=open('myfile.txt','r')
reader=csv.reader(f)
print [x for x in reader] #  This outputs the contents of "myfile.txt",
                          #  broken up by line.
print [x for x in reader] #  This line prints an empty list.

为什么会这样?出于某种原因,阅读器对象只能使用一次?

Why is this happening? Is there some reason the reader object can only be used once?

此处原因相同:

>>> li=[1,2,3,4,5,6,7,8,9]
>>> it=iter(li)
>>> print [x for x in it], [x for x in it]
[1, 2, 3, 4, 5, 6, 7, 8, 9], []

注意空白列表...

csv.reader是迭代器,它可以从容器或序列一个接一个地进行,直到 StopIteration 异常表明存在该容器或序列为止没有更多的项目了.

csv.reader is an iterator that produces items from a container or sequence one by one until the StopIteration exception indicates there are no more items.

对于内置类型(以及我所知道的所有库类型,如csv),迭代是一种方法,而返回"的唯一方法是保留您感兴趣的项或重新创建迭代器.

For built-in types (and all library types like csv that I know of), iteration is one way, and the only way to 'go back' is to keep the items you are interested in or recreate the iterator.

我想可以通过向后搜索来破解/欺骗csv.reader,但是为什么这样做呢?

You can hack/fool csv.reader by doing a backwards seek I suppose, but why do this?

如果需要,您可以复制迭代器:

You can make a copy of an iterator if you need to:

>>> it_copy=list(it)
>>> print [x for x in it_copy],[x for x in it_copy]
[1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9]

或使用 itertools.tee (如Mark Ransom所述).

Or use itertools.tee as Mark Ransom notes.

最好的方法是仅通过迭代器的单程旅行来设计算法.更少的内存,通常更快.

The best is to just design your algorithm around a one-way trip through an iterator. Less memory and often faster.