关于python中readlines函数的参数hint的相关知识总结


Posted in Python onJune 24, 2021

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

效果

关于python中readlines函数的参数hint的相关知识总结

到此这篇关于关于python中readlines函数的参数hint的相关知识总结的文章就介绍到这了,更多相关python readlines函数内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python中字符串的常见操作技巧总结
Jul 28 Python
Python自定义类的数组排序实现代码
Aug 28 Python
Centos7 Python3下安装scrapy的详细步骤
Mar 15 Python
Windows 64位下python3安装nltk模块
Sep 19 Python
在Python中定义一个常量的方法
Nov 10 Python
Python面向对象程序设计OOP深入分析【构造函数,组合类,工具类等】
Jan 05 Python
Python机器学习算法库scikit-learn学习之决策树实现方法详解
Jul 04 Python
PIL图像处理模块paste方法简单使用详解
Jul 17 Python
Numpy将二维数组添加到空数组的实现
Dec 05 Python
Python计算信息熵实例
Jun 18 Python
利用python批量爬取百度任意类别的图片的实现方法
Oct 07 Python
通过python-pptx模块操作ppt文件的方法
Dec 26 Python
详解Python为什么不用设计模式
linux中nohup和后台运行进程查看及终止
Jun 24 #Python
Python面向对象之成员相关知识总结
Jun 24 #Python
Python面向对象之内置函数相关知识总结
Jun 24 #Python
python面向对象版学生信息管理系统
Python实现学生管理系统(面向对象版)
Jun 24 #Python
Pycharm连接远程服务器并远程调试的全过程
You might like
数字转英文
2006/12/06 PHP
浅析PHP中json_encode与json_decode的区别
2020/07/15 PHP
JS保存和删除cookie操作 判断cookie是否存在
2013/11/13 Javascript
JQuery 使用attr方法实现下拉列表选中
2014/10/13 Javascript
AngularJS基础学习笔记之表达式
2015/05/10 Javascript
Vue SPA单页应用首屏优化实践
2018/06/28 Javascript
JavaScript引用类型Object常见用法实例分析
2018/08/08 Javascript
JavaScript实现图片合成下载的示例
2020/11/19 Javascript
初步探究Python程序的执行原理
2015/04/11 Python
使用Python编写一个最基础的代码解释器的要点解析
2016/07/12 Python
python解析基于xml格式的日志文件
2017/02/25 Python
为什么选择python编程语言入门黑客攻防 给你几个理由!
2018/02/02 Python
python3.4+pycharm 环境安装及使用方法
2019/06/13 Python
查看端口并杀进程python脚本代码
2019/12/17 Python
python GUI库图形界面开发之PyQt5简单绘图板实例与代码分析
2020/03/08 Python
Numpy(Pandas)删除全为零的列的方法
2020/09/11 Python
pycharm配置python 设置pip安装源为豆瓣源
2021/02/05 Python
HTML中fieldset标签概述及使用方法
2013/02/01 HTML / CSS
基于IE10/HTML5 开发
2013/04/22 HTML / CSS
canvas简易绘图的实现(海绵宝宝篇)
2018/07/04 HTML / CSS
巴西最大的家电和百货零售商:Casas Bahia
2016/11/22 全球购物
美国首屈一指的高品质珠宝设计师和零售商:Allurez
2018/01/23 全球购物
来自Ocado的宠物商店:Fetch
2018/07/10 全球购物
中级会计职业生涯规划范文
2014/01/16 职场文书
建筑结构施工专业推荐信
2014/02/21 职场文书
幼儿园家长寄语
2014/04/02 职场文书
C++程序员求职信范文
2014/04/14 职场文书
大学生社团活动总结
2014/04/26 职场文书
产品质量保证书
2014/04/29 职场文书
公安纪律作风整顿剖析材料
2014/10/10 职场文书
门面房租房协议书
2014/12/01 职场文书
坎儿井导游词
2015/02/09 职场文书
法制教育观后感
2015/06/17 职场文书
《中华上下五千年》读后感3篇
2019/11/29 职场文书
vue-cli3.x配置全局的scss的时候报错问题及解决
2022/04/30 Vue.js
解决spring.thymeleaf.cache=false不起作用的问题
2022/06/10 Java/Android