Python 标准库 fileinput与文件迭代器
作者:wohu1104 时间:2023-10-31 22:36:50
1. 使用 fileinput 进行迭代
fileinput
模块可以对一个或多个文件中的内容进行迭代、遍历等操作。该模块的 input()
函数有点类似文件 readlines
函数,但区别明显:
readlines
是一次性读取文件中的全部内容,如果文件较大的话,会占用大量内存;input
返回的则是一个迭代对象,结合for
循环使用,典型用法如下:
import fileinput
for line in fileinput.input(filename):
process(line)
实例:
import fileinput
for line in fileinput.input("D:/Users/data.txt"):
print(line)
注意:input
函数有多个参数:分别设置读取文件路径、读写模式、编码方式、缓冲区大小、备份文件扩展名等。
此外,fileinput
模块还有很多常用的函数,这里介绍几个最常用的。
函数名 说明:
fileinput.input()
返回能够用于for
循环遍历的对象fileinput.filename()
返回当前文件的名称fileinput.lineno()
返回当前已经读取的行的数量(或者序号)fileinput.filelineno()
回当前读取的行的行号fileinput.isfirstline()
检查当前行是否是文件的第一行fileinput.isstdin()
判断最后一行是否从stdin
中读取fileinput.close()
关闭队列
2. 文件迭代器
#打开文件,一次写入多行内容
f = open("D:/Users/data.txt",'w')
content = ['append information1\n','append information2']
f.writelines(content)
f.close()
#打开文件,通过文件迭代器遍历文件
f = open("D:/Users/data.txt",'r')
for line in f:
print('content:',line)
# 或者 将读取内容转化为字符串列表 效果类似 readlines
lines = list(f)
print(lines)
#关闭文件
f.close()
执行结果(省略空行):
content: append information1
content: append information2
3. 字典的 setdefault 方法使用举例
如果仅当字典中不存在某个键值对时,才插入到字典中;如果存在,不必插入(也就不会修改键值对)。
这种场景,使用字典自带方法 setdefault
:
In [35]: d = {'a':1,'b':2}
In [36]: r = d.setdefault('c',3) # r: 3
In [37]: r
Out[37]: 3
In [38]: d
Out[38]: {'a': 1, 'b': 2, 'c': 3}
In [39]: r = d.setdefault('c',33) # r:3,已经存在'c':3的键值对,所以setdefault时d无改变
In [40]: r
Out[40]: 3
In [41]: d
Out[41]: {'a': 1, 'b': 2, 'c': 3}
来源:https://blog.csdn.net/wohu1104/article/details/125055526
标签:Python,fileinput,文件,迭代器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php中preg_match的isU代表什么意思
2024-05-03 15:13:51
django haystack实现全文检索的示例代码
2021-04-08 05:00:59
![](https://img.aspxhome.com/file/2023/5/128285_0s.png)
python实现输入三角形边长自动作图求面积案例
2023-08-12 01:47:49
![](https://img.aspxhome.com/file/2023/7/94177_0s.jpg)
pytorch加载预训练模型与自己模型不匹配的解决方案
2023-06-17 14:22:24
JavaScript属性操作
2024-04-16 09:52:52
![](https://img.aspxhome.com/file/2023/9/136509_0s.jpg)
Python 图像处理之PIL库详解用法
2023-05-02 18:06:50
![](https://img.aspxhome.com/file/2023/1/90671_0s.jpg)
Python可视化神器pyecharts绘制漏斗图
2023-01-09 08:40:09
![](https://img.aspxhome.com/file/2023/2/100962_0s.png)
Vue3 计算属性的用法详解
2024-04-28 09:20:33
![](https://img.aspxhome.com/file/2023/3/133113_0s.gif)
Python入门:认识列表和元组
2022-11-06 02:18:42
sqlserver、mysql获取连接字符串步骤
2024-01-22 00:49:33
Asp.net 连接MySQL的实现代码[]
2024-01-16 04:27:15
GPU版本安装Pytorch的最新方法步骤
2022-02-09 16:31:28
![](https://img.aspxhome.com/file/2023/7/107037_0s.jpg)
使用OpenCV实现人脸图像卡通化的示例代码
2023-01-03 13:38:49
![](https://img.aspxhome.com/file/2023/7/82527_0s.jpg)
一文带你深入了解Python中的二次移动平均法
2021-08-30 00:28:12
![](https://img.aspxhome.com/file/2023/1/101211_0s.png)
基于Python实现简单的汉字拼音转换工具
2022-10-03 10:42:35
js 浏览本地文件夹系统示例代码
2024-04-17 09:48:23
Django unittest 设置跳过某些case的方法
2022-02-28 21:46:13
python 实现数据库中数据添加、查询与更新的示例代码
2023-10-08 04:09:51
Mysql带And关键字的多条件查询语句
2024-01-14 08:41:17
![](https://img.aspxhome.com/file/2023/9/83169_0s.png)
jquery ajax 局部无刷新更新数据的实现案例
2024-05-02 17:05:08