濮阳杆衣贸易有限公司

主頁 > 知識庫 > 關(guān)于python中readlines函數(shù)的參數(shù)hint的相關(guān)知識總結(jié)

關(guān)于python中readlines函數(shù)的參數(shù)hint的相關(guān)知識總結(jié)

熱門標(biāo)簽:如何地圖標(biāo)注公司 電銷機器人錄音要學(xué)習(xí)什么 長春極信防封電銷卡批發(fā) 上海正規(guī)的外呼系統(tǒng)最新報價 銀川電話機器人電話 外賣地址有什么地圖標(biāo)注 企業(yè)彩鈴地圖標(biāo)注 煙臺電話外呼營銷系統(tǒng) 預(yù)覽式外呼系統(tǒng)

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 來控制讀取的行數(shù):如果到目前為止所有行的總大小(以字節(jié)/字符為單位)超過hint,則不會讀取更多行。

readme.txt中的內(nèi)容

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

為了進一步搞清楚hint,我寫了一個函數(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,先把每一個元素輸出                
            textline=len(ss)#文件的總行數(shù)
            ntotalbyte=0#文件的總字數(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é)個數(shù)
                while nwritebytentotalbyte:#當(dāng)已寫字節(jié)總字節(jié)數(shù)
                    print(f'{nwritebyte+1}:',repr(ss[j][rowbyte])) #repr是為了輸出換行符
                    nwritebyte=nwritebyte+1
                    rowbyte=rowbyte+1
            print(f'行數(shù)={textline},字數(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,字數(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 默認等于-1,就是以列表的形式讀出所有內(nèi)容

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

4.hint 所指的字節(jié)數(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,先把每一個元素輸出
                print(ss)
                textline=len(ss)#文件的總行數(shù)
                ntotalbyte=0#文件的總字數(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é)個數(shù)
                    while nwritebytentotalbyte:#當(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},字數(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}') #是左對齊

效果

參考文章:https://www.jb51.net/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)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python 整行讀取文本方法并去掉readlines換行\(zhòng)n操作
  • python: line=f.readlines()消除line中\(zhòng)n的方法
  • Python File readlines() 使用方法
  • 詳談python read readline readlines的區(qū)別
  • Python中read()、readline()和readlines()三者間的區(qū)別和用法
  • Python中read,readline和readlines的區(qū)別案例詳解

標(biāo)簽:佳木斯 湖北 宜昌 潮州 珠海 上饒 西寧 盤錦

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《關(guān)于python中readlines函數(shù)的參數(shù)hint的相關(guān)知識總結(jié)》,本文關(guān)鍵詞  關(guān)于,python,中,readlines,函數(shù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《關(guān)于python中readlines函數(shù)的參數(shù)hint的相關(guān)知識總結(jié)》相關(guān)的同類信息!
  • 本頁收集關(guān)于關(guān)于python中readlines函數(shù)的參數(shù)hint的相關(guān)知識總結(jié)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    兴业县| 淳安县| 石景山区| 宜州市| 肇庆市| 尤溪县| 林甸县| 常州市| 闻喜县| 且末县| 裕民县| 武义县| 中超| 寿宁县| 视频| 安仁县| 玉田县| 上饶市| 吴旗县| 宝山区| 仁寿县| 定州市| 伊金霍洛旗| 东兴市| 左权县| 商河县| 梁山县| 莫力| 静海县| 贡觉县| 蓬溪县| 成都市| 许昌市| 日照市| 钦州市| 开平市| 阿尔山市| 紫阳县| 介休市| 宁德市| 连州市|