gpt4 book ai didi

Python 的函数 readlines(n) 行为

转载 作者:太空狗 更新时间:2023-10-29 17:46:53 25 4
gpt4 key购买 nike

我读过 the documentation ,但是 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.

另一段引述:

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.

你是对的,它似乎对小文件没有太大作用,这很有趣:

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']

有人可能会认为这是由文档中的以下短语解释的:

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.

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

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

关于Python 的函数 readlines(n) 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14541010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com