如图 将1.txt中每一行中对应的数字对应的英文替换到2.txt中的数字上,求思路
第一个文件读成字典,第二个文件取出第一个数字序号,如果存在第一个文件的字典中则进行替换,替换的方式有很多
num_eng = dict()
with open("1.txt") as f:
line = f.readline().strip()
while line:
temp = line.split()
num_eng[temp[0]] = temp[1]
line = f.readline.strip()
contents = []
with open("2.txt") as f:
line = f.readline().strip()
while line:
contents.append(line)
line = f.readline().strip()
# vlookup替换
new_contents = []
for content in contents:
temp = content.split()
num = temp[0]
if num in num_eng.keys():
new_contents.append(num_eng[num] + " " + temp[1])
print(new_contents)