关于python中readlines函数的参数hint的相关知识总结
作者:飞由于度 发布时间:2023-12-31 02:37:12
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://www.jb51.net/article/206578.htm
来源:https://blog.csdn.net/qq_35629563/article/details/117912503


猜你喜欢
- 最近实现了一些微信的简单玩法 我们可以通过网页版的微信微信网页版,扫码登录后去抓包爬取信息,还可以post去发送信息。》》安装it
- 工作中遇到了要计算两个数百分比的问题,python 2.7 环境。代码:#!/usr/bin/env python#function: 计算
- 正在看的ORACLE教程是:常见数据库系统比较 Oracle数据库。提起数据库,第一个想到的公司,一般都会是Oracle。该公司
- Python的 eval() 允许从基于字符串或基于编译代码的输入中计算任意Python表达式。当从字符串或编译后的代码
- 引子如果遇到了 Must provide secret_key to use csrf错误提醒,原因就是没有设置secret_key ,在代
- Python 风格规范(Google)本项目并非 Google 官方项目, 而是由国内程序员凭热情创建和维护。如果你关注的是 Google
- 简介视图主要内容:URLconf、HttpRequest对象、HttpResponse1)视图接受Web请求并且返回Web响应2)视图就是一
- 目录前言连接管理额外连接管理端口总结前言下面这个报错,相信大多数童鞋都遇见过;那么碰到这个问题,我们应该怎么办呢?在MySQL 5.7及之前
- django1.3新加入了一个静态资源管理的app,django.contrib.staticfiles。在以往的django版本中,静态资
- 我们可向函数传递动态参数,*args,**kwargs,首先我们来看*args,示例如下:1.show(*args)def show(*ar
- 之前在网上看到有人提问,如何在页面上同步显示服务器的时间,其实实现方法有几种,可能 一般人立马就想到可以使用Ajax每隔一秒去请求服务器,然
- 我就废话不多说了,直接上代码吧!from os import listdirimport osfrom time import timeim
- 本文实例讲述了Python使用matplotlib绘图无法显示中文问题的解决方法。分享给大家供大家参考,具体如下:在python中,默认情况
- 继团队的CSS3.0中文手册在国内首发以后,最近风风火火的到处吹起HTML5.0和CSS3.0的春风;似乎在这浏览器互相调侃的年代,成就了一
- 支持聚合函数的方法:提到聚合函数,首先我们要知道的就是这些聚合函数是不能在django中单独使用的,要想在django中使用这些聚合函数,就
- 目录实现加权轮询负载均衡思路加权轮询负载均衡代码测试代码实现加权轮询负载均衡思路代码实现一个加权负载均衡Weight
- 存储和读取ASCII码形式的byte数据Python可以存byte数据到txt,但不要用str的方式直接存,转成数字列表储存,这样方便读取L
- 前言:前面文章讲述了 MySQL 系统中常见的几种日志,其实还有事务相关日志 redo log 和 undo log 没有介绍。相对于其他几
- 前言没有特别幸运,那么请先特别努力,别因为懒惰而失败,还矫情地将原因归于自己倒霉。你必须特别努力,才能显得毫不费力。希望:所以说,树倒了,没
- 话不多说,直接上代码运行截图 1.语音合成------->执行:结果:输入要转换的内容,程序直接帮你把转换好的mp3文件输出