示例:文件将被称为“Sentences.dat
”。它将有这些线路
Name #3 is Bob
Name #7 is Marie
Name #8 is Kate
Name #2 is George
我想将它们保存到一个dictionary
,其中它们的编号是key
,名称是value
。这就是我目前所拥有的
file = open("Sentences.dat","r")
dictNames = {}
line = 1
while True:
content = file.readlines()[line]
split = content.split()
dictNames[split[1]] = split[3]
line = line + 1
这是另一种有效的方法
试试这个。我还在代码中留下了一些注释
结果:{3:'鲍勃',7:'玛丽',8:'凯特',2:'乔治'}
相关问题 更多 >
编程相关推荐