我有一个看起来像这样的文本文件:
test_00000.png 0
test_00001.png 0
test_00002.png 0
test_00003.png 0
test_00004.png 0
test_00005.png 0
test_00006.png 0
test_00007.png 0
test_00008.png 0
test_00009.png 0
test_00010.png 0
我的任务是使用Python替换所有标签0(即,在test_xxxxx.png之后的0,而不是像test_00000.png这样的图像名称中的0)。我的代码如下所示:
f1 = open('Text1.txt','r')
f2 = open('Text2.txt','w')
for line in f1:
for char in line:
if char==" 0 ":
f2.write(' 1 ')
else:
f2.write(char)
f1.close()
f2.close()
但是,此代码为我提供了与原始文件完全相同的输出。我在这里做错了什么,如何解决?
答案 0 :(得分:1)
我建议只是看一下这行的结尾会更容易:
with open('file1', 'r') as f1, open('file2', 'w') as f2:
for line in f1.readlines():
line = line.strip()
if line.endswith(' 0'):
line = line[:-2] + ' 1'
f2.write(line + '\n')
答案 1 :(得分:1)
将{strong> re.sub
与0([\n$]|\Z)
一起使用
正则表达式解释:
0 # matches 0
( # matching group 1
[\n$] # matches newline or end of line
| # OR
\Z # Matches EOF
) # End of matching group 1
带有replace
的 1\1
(将0替换为1,并维护正确的EOL字符)
import re
with open('test.txt') as f, open('out.txt', 'w') as outf:
data = re.sub(r'0([\n$]|\Z)', r'1\1', f.read())
outf.write(data)
out.txt
test_00000.png 1
test_00001.png 1
test_00002.png 1
test_00003.png 1
test_00004.png 1
test_00005.png 1
test_00006.png 1
test_00007.png 1
test_00008.png 1
test_00009.png 1
test_00010.png 1
答案 2 :(得分:0)
如果您可以接受输出文件可以有1个空格,您可以使用pandas来读取它:
import pandas as pd
data = '''\
test_00000.png 0
test_00001.png 0
test_00002.png 0
test_00003.png 0
test_00004.png 0
test_00005.png 0
test_00006.png 0
test_00007.png 0
test_00008.png 0
test_00009.png 0
test_00010.png 0'''
file = pd.compat.StringIO(data) # replace with "path/to/file"
df = pd.read_csv(file, sep='\s+', header=None).replace(0,1)
df.to_csv('out.csv', sep=' ', header=False, index=False)