如何将值从文件插入字典



我需要存储一个文件中的键和另一个文件的值。和第一个文件一样,该文件的第一行是"a",第二个文件的第一行将是20。字典应该是一个:20

试试这样的东西:

dictionary = {}
with open("file1.txt") as file1, open("file2.txt") as file2:
for key, value as zip(file1, file2):
key = key.strip()
value = value.strip() # If all the values must be integers, do int(value.strip())
dictionary[key] = value

最新更新