python中readlines函数例子_Python的函数readlines(n)行为
可選參數(shù)應(yīng)表示從文件中讀取多少(大約)個字節(jié).該文件將被進(jìn)一步讀取,直到當(dāng)前行結(jié)束:
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']
有人可能認(rè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.
但是,即使我嘗試在沒有緩沖的情況下讀取文件,它似乎也沒有改變?nèi)魏螙|西,這意味著其他類型的內(nèi)部緩沖區(qū)意味著:
In [4]: open('hello', 'r', 0).readlines(2)
Out[4]: ['Hello\n', 'there\n', '!\n']
在我的系統(tǒng)上,這個內(nèi)部緩沖區(qū)大小似乎是大約5k字節(jié)/ 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
總結(jié)
以上是生活随笔為你收集整理的python中readlines函数例子_Python的函数readlines(n)行为的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c 对一个mysql数据库进行操作_C/
- 下一篇: 车险到期了可以推迟多久交