python - python从txt文件中删除某些动态行
问题描述
我有几个 txt 文件,其数据行结构如下:
文件 1
Header1, xx, yy
Redundant line 1
Redundant line 2
Redundant line 3
Header2, #012345 (random numbers)
data content (to the end of file)
文件2
Header1, xx, yy
Redundant line 1
Redundant line 2
Redundant line 3
Redundant line 4
Header2, #67891 (random numbers)
data content (to the end of file)
文件 3
Header1, xx, yy
Redundant line 1
Redundant line 2
Header2, #54321 (random numbers)
data content (to the end of file)
预期输出:
对于每个文件,我想删除那些冗余行,只保留 Header1、Header2、#zzzzz 编号的行以及带有数据内容的以下行到文件末尾,并保存到一个新的单个文件,因此每个新文件具有以下数据结构:
Header1, xx, yy
Header2, #zzzzz (keep random numbers from original file)
data content (to the end of file)
我的代码:
我的代码不适用于具有动态冗余行的每个文件,有人可以提供一些建议,谢谢!
with open('File1.txt') as old, open('new_file1.txt', 'w') as new:
lines = old.readlines()
new.writelines(lines[0:1]) #keep Header1
new.writelines(lines[N:]) #keep Header2 and following data content to the end
解决方案
您可以N
使用初始值定义变量1
,并不断增加它,1
直到一行与正则表达式匹配.*?,#\d+
(对于第二个标题):
import re
with open('File1.txt') as old, open('new_file1.txt', 'w') as new:
lines = old.readlines()
new.writelines(lines[:1]) #keep Header1
N = 1
while True:
N += 1
if re.match(".*?,#\d+", lines[N]):
break
new.writelines(lines[N:]) #keep Header2 and following data content to the end
输入文件File1.txt
:
Header1, xx, yy
Redundant line 1
Redundant line 2
Redundant line 3
Header2, #012345 (random numbers)
data content (to the end of file)
输出文件new_file1.txt
:
Header1, xx, yy
Header2, #012345 (random numbers)
data content (to the end of file)
推荐阅读
- azure-devops - 在构建管道上更新 App config xml 文件中的内部版本号
- python - 在python中重复y/n问题的有效方法
- android - 如何在 Android 中添加标题以改造响应
- unit-testing - Kotlintest 与 Mockk 如何清除验证计数
- elasticsearch - ElasticsearchClientException:请求执行失败。调用:状态码 403 来自:POST /index/
- windows - Windows 10 讲述人。键盘快捷键。从当前光标处读取文档
- javascript - Highcharts - 不显示数据
- makefile - Makefile 不处理所有命令
- c - 如何将二维数组作为双指针传递给c中的函数?
- awk - awk 忽略特定列中引号内的字段分隔符,然后将其移动