此代码首先逐行读取文件,然后将每一行拆分为一个二维数组,如下所示:[[a, b], [c, d] ... ]它根据这个数组添加字典条目(mydict[a] = b 等等)。with open("filename", "r") as file: myinput = file.readlines()myinput = [i.split(" ") for i in myinput]mydict = {}for i in myinput: mydict[i[0]] = i[1]正如@wjandrea 指出的那样,您也可以像这样编写上面的代码:with open("filename", "r") as file: mydict = {line.split() for line in file}