试图理解 python csv .next()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14551484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:49:29  来源:igfitidea点击:

Trying to understand python csv .next()

pythoncsvpython-2.x

提问by davidheller

I have the following code that is part of a tutorial

我有以下代码是教程的一部分

import csv as csv
import numpy as np

csv_file_object = csv.reader(open("train.csv", 'rb'))
header = csv_file_object.next()

data = []
for row in csv_file_object:
    data.append(row)
data = np.array(data)

the code works as it is supposed to but it is not clear to me why calling .next()on the file with the variable headerworks. Isn't csv_file_object still the entire file? How does the program know to skip the header row when for row in csv_file_objectis called since it doesn't appear the variable header is ever referenced once defined?

代码按预期工作,但我不清楚为什么.next()使用变量调用文件header有效。csv_file_object 还是整个文件吗?程序如何知道在for row in csv_file_object调用时跳过标题行,因为一旦定义变量标题就不会被引用?

采纳答案by Lev Levitsky

The header row is "skipped" as a result of calling next(). That's how iterators work.

作为调用的结果,标题行被“跳过” next()。这就是迭代器的工作方式。

When you loop over an iterator, its next()method is called each time. Each call advances the iterator. When the forloop starts, the iterator is already at the second row, and it goes from there on.

当您循环迭代器时,next()每次都会调用它的方法。每次调用都会推进迭代器。当for循环开始时,迭代器已经在第二行,它从那里开始。

Here's the documentationon the next()method (here's another piece).

这是有关该方法的文档next()这是另一篇文章)。

What's important is that csv.readerobjects are iterators, just like file object returned by open(). You can iterate over them, but they don't contain all of the lines (or any of the lines) at any given moment.

重要的是csv.reader对象是迭代器,就像open(). 您可以迭代它们,但它们不包含任何给定时刻的所有行(或任何行)。

回答by Peter Wooster

csv.reader is an iterator. It reads a line from the csv every time that .next is called. Here's the documentation: http://docs.python.org/2/library/csv.html. An iterator object can actually return values from a source that is too big to read all at once. using a for loop with an iterator effectively calls .next on each time through the loop.

csv.reader 是一个迭代器。每次调用 .next 时,它都会从 csv 中读取一行。这是文档:http: //docs.python.org/2/library/csv.html。迭代器对象实际上可以从太大而无法一次读取的源返回值。使用带有迭代器的 for 循环在每次循环中有效地调用 .next 。

回答by Sylvain Defresne

The csv.readerobject is an iterator. An iterator is an object with a next()method that will return the next value available or raise StopIterationif no value is available. The csv.readerwill returns value line by line.

csv.reader对象是一个迭代器。迭代器是一个带有next()方法的对象,该方法将返回下一个可用StopIteration值,如果没有可用值则引发。该csv.reader会一行返回值线。

The iterators objects are how python implements forloop. At the beginning of the loop, the __iter__object of the looped over object will be called. It must return an iterator. Then, the nextmethod of that object will be called and the value stored in the loop variable until the nextmethod raises StopIterationexception.

迭代器对象是 python 实现for循环的方式。在循环开始时,__iter__将调用循环对象的对象。它必须返回一个迭代器。然后,该next对象的方法将被调用并将值存储在循环变量中,直到该next方法引发StopIteration异常。

In your example, by adding a call to next before using the variable in the forloop construction, you are removing the first value from the stream of values returned by the iterator.

在您的示例中,通过在for循环构造中使用变量之前添加对 next 的调用,您将从迭代器返回的值流中删除第一个值。

You can see the same effect with simpler iterators:

您可以使用更简单的迭代器看到相同的效果:

iterator = [0, 1, 2, 3, 4, 5].__iter__()
value = iterator.next()
for v in iterator:
    print v,
1 2 3 4 5
print value
0

回答by Matt Alcock

The csv.reader is an iterator. Calling .next() will obtain the next value as it iterates through the file.

csv.reader 是一个迭代器。调用 .next() 将在遍历文件时获取下一个值。

In the below code the for loop is calling .next() on the iterator each time and allocating the result of next to the variable row.

在下面的代码中,for 循环每次都在迭代器上调用 .next() 并将结果分配到变量行旁边。

for row in csv_file_object:
    data.append(row)

回答by Alain Abrahan

The behavior of next() is more than that, all expose above is ok but there is one thing missing, also using next you are telling the iterator from what line you want to begin the iteration so is a problem let's say that I want some value that is in line 3 without going trough all the lines i can easily use next I got the value, but if I need to iterate on the first line in my case I can't because no matter what the iterator still starting at line 3 so I can not start from line 1 well there is a way but I didn't find it yet.

next() 的行为不仅如此,上面的所有公开都可以,但是缺少一件事,同样使用 next 你告诉迭代器你想从哪一行开始迭代所以这是一个问题,假设我想要一些第 3 行中的值无需遍历我接下来可以轻松使用的所有行我得到了值,但是如果我需要在我的情况下在第一行迭代我不能因为无论迭代器仍然从第 3 行开始所以我不能从第 1 行开始,有一种方法,但我还没有找到。