python 同时读取多个文件的例子
作者:想做码农 时间:2022-06-27 03:56:50
Python中打开文本使用的是with语句,比如打开一个文件并读取每一行
with open(filename) as fp:
for line in fp:
# do something
为了同时读取多个文件,可以使用下面的代码
with open(filename1) as fp1, open(filename2) as fp2, open(filename3) as fp3:
for l1 in fp1:
l2 = fp2.readline()
l3 = fp3.readline()
# do something
稍微简介一点可以使用contextlib中的nested,有
from contextlib import nested
with nested(open(filename1), open(filename2), open(filename3)) as (fp1, fp2, fp3):
for l1 in fp1:
l2 = fp2.readline()
l3 = fp3.readline()
# do something
来源:https://blog.csdn.net/jing186/article/details/72626620
标签:python,读取,文件
0
投稿
猜你喜欢
php引用和拷贝的区别知识点总结
2023-11-15 03:39:48
python用10行代码实现对黄 色图片的检测功能
2023-11-16 23:48:33
如何利用python turtle绘图自定义画布背景颜色
2021-08-02 17:28:49
React redux 原理及使用详解
2023-07-14 15:41:58
Python数据分析之缺失值检测与处理详解
2021-10-04 01:09:08
什么是好的设计
2010-02-25 12:22:00
关于捕获用户何时点击window.onbeforeunload的取消事件
2024-04-22 22:45:07
jquery效率探索
2008-01-08 18:06:00
用SQL语句解决mysql导入大数据文件的问题
2024-01-13 05:52:23
PHP 简单日历实现代码
2023-07-01 12:00:01
Python使用pandas导入xlsx格式的excel文件内容操作代码
2022-03-12 04:29:57
Python生成二维码的教程详解
2023-05-31 08:41:43
解决Python发送Http请求时,中文乱码的问题
2021-02-02 05:11:39
python手写均值滤波
2022-03-16 20:53:19
Go中如何使用set的方法示例
2024-04-28 10:47:19
VUE预渲染及遇到的坑
2023-07-02 17:08:34
python字典操作实例详解
2021-05-21 08:22:24
Python实现按中文排序的方法示例
2023-11-29 15:19:22
fso怎样判断一个盘上是否有文件
2007-09-26 12:35:00
Bootstrap fileinput 上传新文件移除时触发服务器同步删除的配置
2024-04-19 09:45:02