av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

關于python中readlines函數的參數hint的相關知識總結

瀏覽:64日期:2022-06-15 18:04:02
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 實例的 readlines(hint=-1, /) 方法 從流中返回行列表。 可以指定 hint 來控制讀取的行數:如果到目前為止所有行的總大小(以字節/字符為單位)超過hint,則不會讀取更多行。

readme.txt中的內容

關于python中readlines函數的參數hint的相關知識總結

>>> f=open(’readme.txt’)>>> f.readlines()[’1n’, ’22n’, ’n’, ’333’]

為了進一步搞清楚hint,我寫了一個函數來演示

readlines函數代碼

def readlinesFile(filename,nbyte): ’’’ 探索f.readlines(i)中i的作用,典型的調用形式: readlinesFile(’readme.txt’,12) ’’’ for i in range(nbyte):f=open(filename)ss=f.readlines(i) if i==0:#如果hint=0,先把每一個元素輸出 textline=len(ss)#文件的總行數 ntotalbyte=0#文件的總字數 nwritebyte=0#已經寫了的字節數 for j in range(textline):#nwritebyte=ntotalbyte#已經寫了的字節數ntotalbyte=ntotalbyte+len(ss[j])rowbyte=0#已經寫了的新行的字節數,用來記一行已經輸出的字節個數while nwritebyte<ntotalbyte:#當已寫字節<總字節數 print(f’{nwritebyte+1}:’,repr(ss[j][rowbyte])) #repr是為了輸出換行符 nwritebyte=nwritebyte+1 rowbyte=rowbyte+1 print(f’行數={textline},字數={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’行數=4,字數=9f.readlines0=[’1n’, ’22n’, ’n’, ’333’]f.readlines1=[’1n’]f.readlines2=[’1n’, ’22n’]f.readlines3=[’1n’, ’22n’]f.readlines4=[’1n’, ’22n’]f.readlines5=[’1n’, ’22n’, ’n’]f.readlines6=[’1n’, ’22n’, ’n’, ’333’]f.readlines7=[’1n’, ’22n’, ’n’, ’333’]f.readlines8=[’1n’, ’22n’, ’n’, ’333’]f.readlines9=[’1n’, ’22n’, ’n’, ’333’]f.readlines10=[’1n’, ’22n’, ’n’, ’333’]f.readlines11=[’1n’, ’22n’, ’n’, ’333’]

總結:

1.hint 是要輸出顯示的字節數

2.hint 默認等于-1,就是以列表的形式讀出所有內容

3.hint = 0時,效果等同于-1

4.hint 所指的字節數正好是換行符的話,則實際輸出是 hint+1

更花哨的readlinesFile

def readlinesFile(filename,nbyte): ’’’ 探索f.readlines(i)中i是指什么,典型的調用形式: readlinesFile(’readme.txt’,12) ’’’ specialByte=[]#存儲特殊的字節數用 for i in range(nbyte):with open(filename) as f:#使用with語句就可以不使用f.close()了 ss=f.readlines(i) if(i==0):#如果hint=0,先把每一個元素輸出print(ss)textline=len(ss)#文件的總行數ntotalbyte=0#文件的總字數nwritebyte=0#已經寫了的字節數for j in range(textline): #nwritebyte=ntotalbyte#已經寫了的字節數 ntotalbyte=ntotalbyte+len(ss[j]) rowbyte=0#已經寫了的新行的字節數,用來記一行已經輸出的字節個數 while nwritebyte<ntotalbyte:#當已寫字節<總字節數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是字體和背景顏色設置,注意可能需要其他庫的支持else: print(f’{nwritebyte+1:2d}:’,repr(ss[j][rowbyte])) nwritebyte=nwritebyte+1 rowbyte=rowbyte+1print(f’033[0;31;40m行數={textline:2d},字數={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}’) #<是左對齊

效果

關于python中readlines函數的參數hint的相關知識總結

參考文章:https://www.jb51.net/article/206578.htm

到此這篇關于關于python中readlines函數的參數hint的相關知識總結的文章就介紹到這了,更多相關python readlines函數內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 免费在线观看黄色片 | 青青草视频污 | 精品久久影院 | 免费成人黄色 | 久久久久久爱 | 成人黄色在线 | 久久少妇| 毛片视频免费观看 | 国产亚洲视频在线观看 | 国产一级黄 | 久久激情综合 | 国产福利在线 | 播播激情网| 91av在线免费观看 | 日韩理论在线 | 国产a久久麻豆入口 | 玖草视频 | 一色桃子av | 无套内谢的新婚少妇国语播放 | 中文字幕免费看 | 99国产精品99久久久久久 | 在线观看国产一区二区 | a视频 | 日b免费视频 | www.精品| 91免费国产 | 99视频 | 丰满女人裸体淫交 | 国产一区二区中文字幕 | 黄色成人毛片 | 日韩福利一区 | 免费的一级片 | a级片网站| 日本中文字幕在线观看 | 亚洲三级网 | 免费一区二区三区 | 日本中文在线观看 | 一级片免费观看 | 中文字幕在线看片 | 亚洲精品一区二三区 | 91精品国产乱码久久久久 |