日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中readlines函数例子_Python的函数readlines(n)行为

發布時間:2024/10/12 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中readlines函数例子_Python的函数readlines(n)行为 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

可選參數應表示從文件中讀取多少(大約)個字節.該文件將被進一步讀取,直到當前行結束:

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函数例子_Python的函数readlines(n)行为的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。