如何从文件向字典中插入值

问题描述 投票:0回答:1

我需要存储1个文件中的密钥和另一个文件中的值。与第1个文件一样,该文件的第一行是'a',第二个文件的第一行是20。字典应该是a:20

python dictionary file-handling
1个回答
0
投票

尝试这样的事情:

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
© www.soinside.com 2019 - 2024. All rights reserved.