我读了 文件,但readlines(n)做了什么?通过readlines(n),我的意思是readlines(3)或任何其他数字。
当我运行readlines(3)时,它返回与readlines()相同的内容。
我读了 文件,但readlines(n)做了什么?通过readlines(n),我的意思是readlines(3)或任何其他数字。
当我运行readlines(3)时,它返回与readlines()相同的内容。
可选参数应表示从文件中读取多少(大约)个字节。该文件将被进一步读取,直到当前行结束:
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.
另一个引用:
如果给出可选参数 sizehint,它从文件中读取了许多字节,并且足以完成一行,并从中返回行。
你是对的,它似乎对小文件没有太大作用,这很有趣:
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']
有人可能认为文档中的以下短语解释了它:
使用readline()读取EOF并返回包含如此读取的行的列表。如果存在可选的sizehint参数,而不是读取到EOF,则整行总共大小为sizehint字节 (可能在四舍五入到内部缓冲区大小之后) 正在阅读实现类文件接口的对象可以选择忽略sizehint,如果它无法实现,或者无法有效实现。
但是,即使我尝试在没有缓冲的情况下读取文件,它似乎也没有改变任何东西,这意味着其他类型的内部缓冲区意味着:
In [4]: open('hello', 'r', 0).readlines(2)
Out[4]: ['Hello\n', 'there\n', '!\n']
在我的系统上,这个内部缓冲区大小似乎是大约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
可选参数应表示从文件中读取多少(大约)个字节。该文件将被进一步读取,直到当前行结束:
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.
另一个引用:
如果给出可选参数 sizehint,它从文件中读取了许多字节,并且足以完成一行,并从中返回行。
你是对的,它似乎对小文件没有太大作用,这很有趣:
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']
有人可能认为文档中的以下短语解释了它:
使用readline()读取EOF并返回包含如此读取的行的列表。如果存在可选的sizehint参数,而不是读取到EOF,则整行总共大小为sizehint字节 (可能在四舍五入到内部缓冲区大小之后) 正在阅读实现类文件接口的对象可以选择忽略sizehint,如果它无法实现,或者无法有效实现。
但是,即使我尝试在没有缓冲的情况下读取文件,它似乎也没有改变任何东西,这意味着其他类型的内部缓冲区意味着:
In [4]: open('hello', 'r', 0).readlines(2)
Out[4]: ['Hello\n', 'there\n', '!\n']
在我的系统上,这个内部缓冲区大小似乎是大约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
它列出了 线,给定的字符大小'n'跨越 从当前行开始。
例如:在 text
文件,内容为
one
two
three
four
open('text').readlines(0)
回报 ['one\n', 'two\n', 'three\n', 'four\n']
open('text').readlines(1)
回报 ['one\n']
open('text').readlines(3)
回报 ['one\n']
open('text').readlines(4)
回报 ['one\n', 'two\n']
open('text').readlines(7)
回报 ['one\n', 'two\n']
open('text').readlines(8)
回报 ['one\n', 'two\n', 'three\n']
open('text').readlines(100)
回报 ['one\n', 'two\n', 'three\n', 'four\n']
根据文件的大小,readlines(提示)应返回一组较小的行。从文档:
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.
因此,如果你的文件有1000行,你可以传入说... 65536,它一次只能读取那么多字节+足以完成下一行,返回所有完全读取的行。