python - Python remove all lines starting with pattern
问题描述
So I have 7000+ txt files that look something like this:
1 0.51 0.73 0.81
0 0.24 0.31 0.18
2 0.71 0.47 0.96
1 0.15 0.25 0.48
And as output I want:
0 0.24 0.31 0.18
2 0.71 0.47 0.96
I wrote the code combining multiple sources and it looks like this:
#!/usr/bin/env python3
2 import glob
3 import os
4 import pathlib
5 import re
6 path = './*.txt'
7
8 for filename in glob.glob(path):
9 with open(filename, 'r') as f:
10 for line in f.readlines():
13 if not (line.startswith('1')):
14 print(line)
15 out = open(filename, 'w')
16 out.write(line)
17 f.close()
But the output for the upper example is:
2 0.71 0.47 0.96
How can I fix the code to give me the correct output?
解决方案
This is because you overwrite the output in the for-loop. You can either save to a different file:
path = 'test.txt'
output = 'out.txt'
for filename in glob.glob(path):
with open(filename, 'r') as f:
out = open(outfile, 'w')
for line in f.readlines():
if not (line.startswith('1')):
print(line)
out.write(line)
f.close()
or you can use append to make an array and then write that to the same file:
import glob
import os
import pathlib
import re
path = 'test.txt'
output = []
for filename in glob.glob(path):
with open(filename, 'r') as f:
for line in f.readlines():
if not (line.startswith('1')):
print(line)
output.append(line)
with open(path, 'w') as w:
for line in output:
print(line)
w.write(line)
f.close()
推荐阅读
- php - 如何获得无需每次登录都可以使用的 facebook 访问令牌?
- javascript - Parse - 查询数组中包含的用户对象
- uima - 升级到 Ruta 2.8.0 后无法创建 Ruta 注释
- javascript - 单击外部时关闭下拉菜单
- json - 如何使用 Power Query 从 Toggl API 中提取数据?
- regex - 如何匹配字符串直到某个模式(Python 3.8)
- tsql - 计算多列的特定值
- javascript - 如何使用 Angular @HostListener onLoad 来获取视口的当前宽度?
- wildfly - Wildfly 18 Jackson2 ObjectMapper 被忽略
- html - Angular - 构建一个 ActionBar - 当当前容器中没有空间时将元素移动到不同的容器