Python 中的 `next(f)`、`f.readline()` 和 `f.next()` 有什么区别?

What are the differences among `next(f)`, `f.readline()` and `f.next()` in Python?

我处理一个文件:跳过header(注释),处理第一行,处理其他行。

f = open(filename, 'r')

# skip the header
next(f)  

# handle the first line
line =  next(f)  
process_first_line(line)

# handle other lines
for line in f:
    process_line(line)

如果把line = next(f)换成line = f.readline(),就会报错

ValueError: Mixing iteration and read methods would lose data

所以想知道next(f)f.readline()f.next()在Python中的区别?

引用 official Python documentation,

A file object is its own iterator, for example iter(f) returns f (unless f is closed). When a file is used as an iterator, typically in a for loop (for example, for line in f: print line.strip()), the next() method is called repeatedly. This method returns the next input line, or raises StopIteration when EOF is hit when the file is open for reading (behavior is undefined when the file is open for writing). In order to make a for loop the most efficient way of looping over the lines of a file (a very common operation), the next() method uses a hidden read-ahead buffer. As a consequence of using a read-ahead buffer, combining next() with other file methods (like readline()) does not work right.

基本上,当在 Python 的文件对象上调用 next 函数时,它会从文件中获取一定数量的字节并处理它们,returns 只有当前行(当前行的结尾由换行符决定)。所以,文件指针被移动了。它不会在当前返回行结束的同一位置。因此,对其调用 readline 会产生不一致的结果。这就是为什么不允许将两者混用的原因。