關(guān)于python中readlines函數(shù)的參數(shù)hint的相關(guān)知識總結(jié)
readlines的幫助信息
>>> fr=open('readme.txt') >>> help(fr.readlines) Help on built-in function readlines: readlines(hint=-1, /) method of _io.TextIOWrapper instance Return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.
Google翻譯
_io.TextIOWrapper 實(shí)例的 readlines(hint=-1, /) 方法
從流中返回行列表。
可以指定 hint 來控制讀取的行數(shù):如果到目前為止所有行的總大小(以字節(jié)/字符為單位)超過hint,則不會(huì)讀取更多行。
readme.txt中的內(nèi)容
>>> f=open('readme.txt') >>> f.readlines() ['1\n', '22\n', '\n', '333']
為了進(jìn)一步搞清楚hint,我寫了一個(gè)函數(shù)來演示
readlines函數(shù)代碼
def readlinesFile(filename,nbyte): ''' 探索f.readlines(i)中i的作用,典型的調(diào)用形式: readlinesFile('readme.txt',12) ''' for i in range(nbyte): f=open(filename) ss=f.readlines(i) if i==0:#如果hint=0,先把每一個(gè)元素輸出 textline=len(ss)#文件的總行數(shù) ntotalbyte=0#文件的總字?jǐn)?shù) nwritebyte=0#已經(jīng)寫了的字節(jié)數(shù) for j in range(textline): #nwritebyte=ntotalbyte#已經(jīng)寫了的字節(jié)數(shù) ntotalbyte=ntotalbyte+len(ss[j]) rowbyte=0#已經(jīng)寫了的新行的字節(jié)數(shù),用來記一行已經(jīng)輸出的字節(jié)個(gè)數(shù) while nwritebyte<ntotalbyte:#當(dāng)已寫字節(jié)<總字節(jié)數(shù) print(f'{nwritebyte+1}:',repr(ss[j][rowbyte])) #repr是為了輸出換行符 nwritebyte=nwritebyte+1 rowbyte=rowbyte+1 print(f'行數(shù)={textline},字?jǐn)?shù)={ntotalbyte}') print(f'f.readlines{i}={ss}') f.close()
輸出
>>> readlinesFile('readme.txt',12)
1: '1'
2: '\n'
3: '2'
4: '2'
5: '\n'
6: '\n'
7: '3'
8: '3'
9: '3'
行數(shù)=4,字?jǐn)?shù)=9
f.readlines0=['1\n', '22\n', '\n', '333']
f.readlines1=['1\n']
f.readlines2=['1\n', '22\n']
f.readlines3=['1\n', '22\n']
f.readlines4=['1\n', '22\n']
f.readlines5=['1\n', '22\n', '\n']
f.readlines6=['1\n', '22\n', '\n', '333']
f.readlines7=['1\n', '22\n', '\n', '333']
f.readlines8=['1\n', '22\n', '\n', '333']
f.readlines9=['1\n', '22\n', '\n', '333']
f.readlines10=['1\n', '22\n', '\n', '333']
f.readlines11=['1\n', '22\n', '\n', '333']
總結(jié):
1.hint 是要輸出顯示的字節(jié)數(shù)
2.hint 默認(rèn)等于-1,就是以列表的形式讀出所有內(nèi)容
3.hint = 0時(shí),效果等同于-1
4.hint 所指的字節(jié)數(shù)正好是換行符的話,則實(shí)際輸出是 hint+1
更花哨的readlinesFile
def readlinesFile(filename,nbyte): ''' 探索f.readlines(i)中i是指什么,典型的調(diào)用形式: readlinesFile('readme.txt',12) ''' specialByte=[]#存儲特殊的字節(jié)數(shù)用 for i in range(nbyte): with open(filename) as f:#使用with語句就可以不使用f.close()了 ss=f.readlines(i) if(i==0):#如果hint=0,先把每一個(gè)元素輸出 print(ss) textline=len(ss)#文件的總行數(shù) ntotalbyte=0#文件的總字?jǐn)?shù) nwritebyte=0#已經(jīng)寫了的字節(jié)數(shù) for j in range(textline): #nwritebyte=ntotalbyte#已經(jīng)寫了的字節(jié)數(shù) ntotalbyte=ntotalbyte+len(ss[j]) rowbyte=0#已經(jīng)寫了的新行的字節(jié)數(shù),用來記一行已經(jīng)輸出的字節(jié)個(gè)數(shù) while nwritebyte<ntotalbyte:#當(dāng)已寫字節(jié)<總字節(jié)數(shù) if(nwritebyte is ntotalbyte-1): specialByte.append(nwritebyte) print(f'\033[0;31;47m{nwritebyte+1:2d}:',repr(ss[j][rowbyte]),'\033[0m')#\033[0m是字體和背景顏色設(shè)置,注意可能需要其他庫的支持 else: print(f'{nwritebyte+1:2d}:',repr(ss[j][rowbyte])) nwritebyte=nwritebyte+1 rowbyte=rowbyte+1 print(f'\033[0;31;40m行數(shù)={textline:2d},字?jǐn)?shù)={ntotalbyte:2d}\033[0m') if i in specialByte: print(f'\033[0;31;47mf.readlines{i:<2d}={ss}\033[0m') #<是左對齊 else: print(f'f.readlines{i:<2d}={ss}') #<是左對齊
效果
參考文章:http://www.dbjr.com.cn/article/206578.htm
到此這篇關(guān)于關(guān)于python中readlines函數(shù)的參數(shù)hint的相關(guān)知識總結(jié)的文章就介紹到這了,更多相關(guān)python readlines函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)查詢剪貼板自動(dòng)匹配信息的思路詳解
這篇文章主要介紹了Python實(shí)現(xiàn)查詢剪貼板自動(dòng)匹配信息,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07Pyqt5實(shí)現(xiàn)英文學(xué)習(xí)詞典
這篇文章主要為大家詳細(xì)介紹了Pyqt5實(shí)現(xiàn)英文學(xué)習(xí)詞典的相關(guān)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Python利用tkinter實(shí)現(xiàn)一個(gè)簡易番茄鐘的示例代碼
番茄鐘是番茄工作法使用的一個(gè)時(shí)間表,即選擇一個(gè)待完成的任務(wù),將番茄時(shí)間設(shè)為25分鐘,專注工作,中途不允許做任何與該任務(wù)無關(guān)的事,直到番茄時(shí)鐘響起,然后在紙上畫一個(gè)X短暫休息一下。本文用tkinter實(shí)現(xiàn)一個(gè)簡易番茄鐘,需要的可以參考一下2022-12-12解決Python中報(bào)錯(cuò)TypeError: must be str, not bytes問題
這篇文章主要介紹了解決Python中報(bào)錯(cuò)TypeError: must be str, not bytes問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04tensorflow實(shí)現(xiàn)簡單的卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了tensorflow實(shí)現(xiàn)簡單的卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05- 在實(shí)驗(yàn)中需要自己構(gòu)造單獨(dú)的HTTP數(shù)據(jù)報(bào)文,而使用SOCK_STREAM進(jìn)行發(fā)送數(shù)據(jù)包,需要進(jìn)行完整的TCP交互。因此想使用原始套接字進(jìn)行編程,直接構(gòu)造數(shù)據(jù)包,并在IP層進(jìn)行發(fā)送,即采用SOCK_RAW進(jìn)行數(shù)據(jù)發(fā)送。使用SOCK_RAW的優(yōu)勢是,可以對數(shù)據(jù)包進(jìn)行完整的修改,可以處理IP層上的所有數(shù)據(jù)包,對各字段進(jìn)行修改,而不受UDP和TCP的限制。2014-02-02