Python 如何使用re.match()更改文件中的多行
我在输入文件(Python 如何使用re.match()更改文件中的多行,python,Python,我在输入文件(input.txt)中有四个单词,我想在另一个输入文件(file.txt)中搜索这些单词。如果单词与条件匹配,则file.txt的第四列将乘以2。我的代码将数据写入输出文件(file\u edited.txt)4次。这不是我想要的。如何修复我的代码 我的代码: # !/usr/bin/env python3 import os import re import numpy as np out = open("file_edited.txt", "w") with open("in
input.txt
)中有四个单词,我想在另一个输入文件(file.txt
)中搜索这些单词。如果单词与条件匹配,则file.txt
的第四列将乘以2。我的代码将数据写入输出文件(file\u edited.txt
)4次。这不是我想要的。如何修复我的代码
我的代码:
# !/usr/bin/env python3
import os
import re
import numpy as np
out = open("file_edited.txt", "w")
with open("input.txt", mode='r') as f:
for lines in f:
line = lines.strip()
search_str = line
with open('file.txt', mode='r') as infile:
for line in infile:
data = line.rstrip().split()
if (len(data) == 4) and re.match(search_str, line):
data[3] = 2 * float(data[3])
out.write("%s %5s %12s %7.2f\n" % (data[0], data[1], data[2], data[3]))
else:
out.write(line)
out.close()
C1522 1 07.123222 1.98 1.222222
C48D42 9 08.222222 2.13
C48D42 4 07.288822 5.58 5.356359
CC3D51 2 09.227822 2.58 3.568523
CC3752 3 07.333333 4.45
ABCD15 3 07.266222 2.50 5.084582
CC3752 6 07.222222 3.25 4.084582
CC3552 3 07.223222 8.42 8.356359
A52C35 3 09.222222 2.15
A4814C 3 07.222222 2.55 5.256254
A4814C 3 07.222222 3.45
CCD152 3 07.222222 0.00 2.451678
C1522 1 07.123222 1.98 1.222222
C48D42 9 08.222222 4.26
C48D42 4 07.288822 5.58 5.356359
CC3D51 2 09.227822 2.58 3.568523
CC3752 3 07.333333 8.90
ABCD15 3 07.266222 2.50 5.084582
CC3752 6 07.222222 3.25 4.084582
CC3552 3 07.223222 8.42 8.356359
A52C35 3 09.222222 4.30
A4814C 3 07.222222 2.55 5.256254
A4814C 3 07.222222 6.90
CCD152 3 07.222222 0.00 2.451678
input.txt
C48D42
CC3752
A52C35
A4814C
file.txt:
# !/usr/bin/env python3
import os
import re
import numpy as np
out = open("file_edited.txt", "w")
with open("input.txt", mode='r') as f:
for lines in f:
line = lines.strip()
search_str = line
with open('file.txt', mode='r') as infile:
for line in infile:
data = line.rstrip().split()
if (len(data) == 4) and re.match(search_str, line):
data[3] = 2 * float(data[3])
out.write("%s %5s %12s %7.2f\n" % (data[0], data[1], data[2], data[3]))
else:
out.write(line)
out.close()
C1522 1 07.123222 1.98 1.222222
C48D42 9 08.222222 2.13
C48D42 4 07.288822 5.58 5.356359
CC3D51 2 09.227822 2.58 3.568523
CC3752 3 07.333333 4.45
ABCD15 3 07.266222 2.50 5.084582
CC3752 6 07.222222 3.25 4.084582
CC3552 3 07.223222 8.42 8.356359
A52C35 3 09.222222 2.15
A4814C 3 07.222222 2.55 5.256254
A4814C 3 07.222222 3.45
CCD152 3 07.222222 0.00 2.451678
C1522 1 07.123222 1.98 1.222222
C48D42 9 08.222222 4.26
C48D42 4 07.288822 5.58 5.356359
CC3D51 2 09.227822 2.58 3.568523
CC3752 3 07.333333 8.90
ABCD15 3 07.266222 2.50 5.084582
CC3752 6 07.222222 3.25 4.084582
CC3552 3 07.223222 8.42 8.356359
A52C35 3 09.222222 4.30
A4814C 3 07.222222 2.55 5.256254
A4814C 3 07.222222 6.90
CCD152 3 07.222222 0.00 2.451678
所需输出(文件\u edited.txt):
# !/usr/bin/env python3
import os
import re
import numpy as np
out = open("file_edited.txt", "w")
with open("input.txt", mode='r') as f:
for lines in f:
line = lines.strip()
search_str = line
with open('file.txt', mode='r') as infile:
for line in infile:
data = line.rstrip().split()
if (len(data) == 4) and re.match(search_str, line):
data[3] = 2 * float(data[3])
out.write("%s %5s %12s %7.2f\n" % (data[0], data[1], data[2], data[3]))
else:
out.write(line)
out.close()
C1522 1 07.123222 1.98 1.222222
C48D42 9 08.222222 2.13
C48D42 4 07.288822 5.58 5.356359
CC3D51 2 09.227822 2.58 3.568523
CC3752 3 07.333333 4.45
ABCD15 3 07.266222 2.50 5.084582
CC3752 6 07.222222 3.25 4.084582
CC3552 3 07.223222 8.42 8.356359
A52C35 3 09.222222 2.15
A4814C 3 07.222222 2.55 5.256254
A4814C 3 07.222222 3.45
CCD152 3 07.222222 0.00 2.451678
C1522 1 07.123222 1.98 1.222222
C48D42 9 08.222222 4.26
C48D42 4 07.288822 5.58 5.356359
CC3D51 2 09.227822 2.58 3.568523
CC3752 3 07.333333 8.90
ABCD15 3 07.266222 2.50 5.084582
CC3752 6 07.222222 3.25 4.084582
CC3552 3 07.223222 8.42 8.356359
A52C35 3 09.222222 4.30
A4814C 3 07.222222 2.55 5.256254
A4814C 3 07.222222 6.90
CCD152 3 07.222222 0.00 2.451678
每次迭代输入时,在第二个循环中再次写入所有行。尝试将信息作为默认dict存储在数据结构中
# !/usr/bin/env python3
import os
import re
import numpy as np
from collections import defaultdict
# Create a data structure to keep track of values
d = defaultdict(str)
# Here you initialize all default values
# because you just replace what you need
with open('file.txt', mode='r') as infile:
for index, line in enumerate(infile):
d[index] = line
out = open("file_edited.txt", "w")
with open("input.txt", mode='r') as f:
for lines in f:
line = lines.strip()
search_str = line
with open('file.txt', mode='r') as infile:
for index, line in enumerate(infile):
data = line.rstrip().split()
if (len(data) == 4) and re.match(search_str, line):
data[3] = 2 * float(data[3])
# Now you just replace the value if you really need
# If you do not replace the value it will be the default already initialized
d[index] = "%s %5s %12s %7.2f\n" % (data[0], data[1], data[2], data[3])
# Now you just print the values to file without repeat them
for k, v in d.items():
out.write(v)
out.close()
为什么不将file.txt加载为csv并对其进行操作?您是否进行过任何调试?你能解释一下你要做的手术吗?