Python 的函数 readlines(n) 行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14541010/
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:46:53  来源:igfitidea点击:

Python's function readlines(n) behavior

pythonpython-2.7

提问by user2013613

I've read the documentation, but what does readlines(n) do? By readlines(n), I mean readlines(3) or any other number.

我已经阅读了文档,但是 readlines(n) 有什么作用?通过 readlines(n),我的意思是 readlines(3) 或任何其他数字。

When I run readlines(3), it returns same thing as readlines().

当我运行 readlines(3) 时,它返回与 readlines() 相同的内容。

采纳答案by Lev Levitsky

The optional argument should mean how many (approximately) bytes are read from the file. The file will be read further, until the current line ends:

可选参数应该表示从文件中读取了多少(大约)字节。该文件将被进一步读取,直到当前行结束:

readlines([size]) -> list of strings, each a line from the file.

Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.

Another quote:

另一个引用:

If given an optional parameter sizehint, it reads that many bytes from the file and enough more to complete a line, and returns the lines from that.

如果给定一个可选参数sizehint,它会从文件中读取那么多字节以及足够多的字节来完成一行,并从中返回行。

You're right that it doesn't seem to do much for small files, which is interesting:

您说得对,它似乎对小文件没有太大作用,这很有趣:

In [1]: open('hello').readlines()
Out[1]: ['Hello\n', 'there\n', '!\n']

In [2]: open('hello').readlines(2)
Out[2]: ['Hello\n', 'there\n', '!\n']

One might think it's explained by the following phrase in the documentation:

人们可能认为它是由文档中的以下短语解释的:

Read until EOF using readline() and return a list containing the lines thus read. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size)are read. Objects implementing a file-like interface may choose to ignore sizehint if it cannot be implemented, or cannot be implemented efficiently.

使用 readline() 读取直到 EOF 并返回包含由此读取的行的列表。如果存在可选的 sizehint 参数,而不是读取到 EOF,而是读取总计大约 sizehint 字节(可能在向上舍入到内部缓冲区大小之后)的整行。实现类文件接口的对象可能会选择忽略 sizehint,如果它不能被实现,或者不能被有效地实现。

However, even when I try to read the file without buffering, it doesn't seem to change anything, which means some other kind of internal buffer is meant:

然而,即使我尝试在没有缓冲的情况下读取文件,它似乎也没有改变任何东西,这意味着其他类型的内部缓冲区是指:

In [4]: open('hello', 'r', 0).readlines(2)
Out[4]: ['Hello\n', 'there\n', '!\n']

On my system, this internal buffer size seems to be around 5k bytes / 1.7k lines:

在我的系统上,这个内部缓冲区大小似乎约为 5k 字节/1.7k 行:

In [1]: len(open('hello', 'r', 0).readlines(5))
Out[1]: 1756

In [2]: len(open('hello', 'r', 0).readlines())
Out[2]: 28080

回答by billjamesdev

Depending on the size of the file, readlines(hint) should return a smaller set of lines. From the documentation:

根据文件的大小, readlines(hint) 应该返回一组较小的行。从文档:

f.readlines() returns a list containing all the lines of data in the file. 
If given an optional parameter sizehint, it reads that many bytes from the file 
and enough more to complete a line, and returns the lines from that. 
This is often used to allow efficient reading of a large file by lines, 
but without having to load the entire file in memory. Only complete lines 
will be returned.

So, if your file has 1000s of lines, you can pass in say... 65536, and it will only read up to that many bytes at a time + enough to complete the next line, returning all the lines that are completely read.

因此,如果您的文件有 1000 行,您可以传入说... 65536,它一次最多只能读取那么多字节+足以完成下一行,返回所有完全读取的行。

回答by S471

It lists the lines, through which the given character size 'n' spans starting from the current line.

它列出所述线,通过该给定的字符尺寸“N”跨度从当前行开始。

Ex: In a textfile, with content of

例如:在一个text文件中,内容为

one
two
three
four

open('text').readlines(0)returns ['one\n', 'two\n', 'three\n', 'four\n']

open('text').readlines(0)返回 ['one\n', 'two\n', 'three\n', 'four\n']

open('text').readlines(1)returns ['one\n']

open('text').readlines(1)返回 ['one\n']

open('text').readlines(3)returns ['one\n']

open('text').readlines(3)返回 ['one\n']

open('text').readlines(4)returns ['one\n', 'two\n']

open('text').readlines(4)返回 ['one\n', 'two\n']

open('text').readlines(7)returns ['one\n', 'two\n']

open('text').readlines(7)返回 ['one\n', 'two\n']

open('text').readlines(8)returns ['one\n', 'two\n', 'three\n']

open('text').readlines(8)返回 ['one\n', 'two\n', 'three\n']

open('text').readlines(100)returns ['one\n', 'two\n', 'three\n', 'four\n']

open('text').readlines(100)返回 ['one\n', 'two\n', 'three\n', 'four\n']