我正在创建一个非常基本的程序,用于存储从英语翻译为法语的单词词典,例如:
我将所有这些值存储在我使用pickle函数追加并读取的文件中。
问题是当我使用pickle.load读取文件然后打印时,仅返回第一个值。
我看不到我的错误在哪里,而且我一直到处寻找而没有得到任何答案。 预先感谢。
import pickle
import os
clear=lambda:os.system("cls")
def pause():
input("Press ENTER to continue.")
def print_dictionary(dct):
print("Dictionary (English / French)")
for wordenglish, wordfrench in dct.items():
print("{} : {}".format(wordenglish, wordfrench))
dictionary={}
for loop in range(3):
wordEnglish=input("Enter the word in English : ")
wordFrench=input("Enter the word in French : ")
pause()
clear()
print("Saving ...")
dictionary[wordEnglish]=wordFrench
with open("data","ab") as file:
pickler=pickle.Pickler(file)
pickler.dump(dictionary)
print("Saved !")
pause()
with open("data","rb") as file:
unpickler=pickle.Unpickler(file)
dictionary_get=unpickler.load()
print_dictionary(dictionary_get)
pause()
例如,如果我输入“ Fire”,“ Feu” /“ Water”,“ Eau” /“ Mud”,“ Boue”,我将获得的唯一值将是“ Fire”,“ Feu”。 / p>
答案 0 :(得分:1)
使用类似
的异常try:
while True:
y=pickle.load(file)
print(y)
except EOFError:
print('End of File')