详解python中readlines函数的参数hint

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中的内容

>>> f=open('readme.txt')
>>> f.readlines()
['1\n', '22\n', '\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,字数=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']

总结:

  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+1
                print(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}') #<是左对齐

效果

参考文章:https://blog.csdn.net/weixin_44478378/article/details/104967241

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值