【发布时间】:2017-04-20 12:44:52
【问题描述】:
我正在尝试用 file2 中的更正值替换 file1 中存在“错误”的行(见下文)。
文件1:
MAGA 0.0159
TTKI error
MCCN 0.0391
NEFD 0.9982
ESYA error
文件2:
TTKI 0.7652
ESYA 0.5517
期望的输出:
MAGA 0.0159
TTKI 0.7652
MCCN 0.0391
NEFD 0.9982
ESYA 0.5517
以下是我一直在尝试的方法,但我认为我已经走了很远,并且在过去一个小时左右变得越来越沮丧,所以任何帮助都将不胜感激。
section2 = []
f2 = open('file2', 'r')
for line2 in f2:
section2.append(str(line2.split(' ',0)))
f1 = open('file1', 'r')
for line1 in f1:
if str(section2[0]) in line1:
print section2[0]
else:
print line1
【问题讨论】: