好吧,所以现在的问题是我只是无法正确加载文件,因为它应该...当我得到它实际加载它总是只保留一个值,而不是所有的价值。正确地使这个文件加载正确吗?(python 2.7)
lista = {'Cop': '911', 'Police chief': '911'
, 'firemen' : '912', 'fire chef' : '912'
,}
保存功能我想它的工作的罚款(得到了我的问题的代码)
def save(lista):
spara = lista
from collections import defaultdict
d = defaultdict(list)
fil = open("test" + ".txt","w")
for a, b in lista.items():
d[b].append(a)
for a, b in d.items():
fil.write(a+';'+';'.join(b)+"\n")
fil.close()
像下面的负载功能只是想填补了字典'正常',并已全部键和值保存到文件之前。
def load(lista):
ladda = open("test" + ".txt","r")
for namesandnumbers in ladda:
(key,val) = namesandnumbers.split(";")
lista[(val)] = key[:len(key)]
lista = ladda
return lista
while True:
choice = input(" 1 add to list 2 to save 3 to load ")
if choice == 1:
word = raw_input("Type the key: ")
word2 = raw_input("Type the value: ")
lista[word] = word2
print lista
continue
if choice == 2:
save(lista)
elif choice == 3:
load(lista)
elif choice == 4:
False
在它看起来像这样
911文件;警察;警察局长;
912; firemen; fire chef;
问题是如何加载时分裂。
改用咸菜。 https://docs.python.org/2/library/pickle.html – FredMan
@FredMan picke不允许使用。如果是的话就会这样做。 – noname292123
cat(或以其他方式显示)我们保存的文件内容,并解释您得到的错误或您使用加载函数获得的输出。 – FredMan