[英]Using Python's pickle to open and save a dictionary
在我的intro comp sci课程的最后几天,我们开始创建词典。 我们书中的家庭作业程序要求我们创建可以查找,添加,更改和删除一组名称和电子邮件地址的内容。 它要求我们挑选字典,但对我而言,它是规定每次程序启动时,它应该从文件中检索字典并取消它。 我不知道我是否将自己编入角落,但我无法弄清楚到目前为止我是如何做到这一点的。
我的代码:
import mMyUtils
import pickle
LOOK_UP = 1
ADD = 2
CHANGE = 3
DELETE = 4
QUIT = 5
def main():
emails = {}
choice = 0
while choice != QUIT:
choice = getMenuChoice()
if choice == LOOK_UP:
lookUp(emails)
elif choice == ADD:
add(emails)
elif choice == CHANGE:
change(emails)
elif choice == DELETE:
delete(emails)
else:
exit
def getMenuChoice():
print()
print('Name and Email Address Catalog')
print('------------------------------')
print('1. Look up an email address')
print('2. Add a new email address')
print('3. Change an email address')
print('4. Delete an email address')
print('5. Quit the program')
print()
choice = int(input('Enter the choice: '))
while choice < LOOK_UP or choice > QUIT:
choice = int(input('Enter a valid choice: '))
return choice
def lookUp(emails):
name = input('Enter a name: ')
print(emails.get(name, 'Not found.'))
def add(emails):
name = input('Enter a name: ')
address = input('Enter an email address: ')
if name not in emails:
emails[name] = address
pickle.dump(emails, open("emails.dat", "wb"))
else:
print('That entry already exists.')
def change(emails):
name = input('Enter a name: ')
if name in emails:
address = input('Enter the new address: ')
emails[name] = address
pickle.dump(emails, open("emails.dat", "wb"))
else:
print('That name is not found.')
def delete(emails):
name = input('Enter a name: ')
if name in emails:
del emails[name]
else:
print('That name is not found.')
main()
我知道我应该将我的电子邮件变量设置为某种形式的pickle.load,但我无法弄清楚我的生活。 mMyUtils是我为try / except逻辑创建的库,我会把它放在一次我得到新的东西工作。
如果你像这样保存字典:
pickle.dump(emails, open('emails.dat', 'wb'))
以下将加载它:
emails = pickle.load(open('emails.dat', 'rb'))
您必须先加载文件并lookUp()
数据,然后才能访问它,将lookUp()
更改为:
def lookUp(emails):
with open("emails.dat", "rb") as fo:
emails = pickle.load(fo)
name = input('Enter a name: ')
print(emails.get(name, 'Not found.'))
考虑自己使用ast.literal_eval而不是pickle: http ://docs.python.org/2/library/ast.html#ast.literal_eval
>>>import ast
>>> print mydict
{'bob': 1, 'danny': 3, 'alan': 2, 'carl': 40}
>>> string="{'bob': 1, 'danny': 3, 'alan': 2, 'carl': 40}"
>>> type(string)
<type 'str'>
>>> type( ast.literal_eval(string) )
<type 'dict'>
要从文件中保存/读取dict,可以像使用普通字符串一样进行。
问题是,我想我没有足够强调,如果字典首先不存在,我本来应该做的。 设计文档声明每次运行程序时都应加载字典。 好吧,如果你是第一次运行程序,你没有要加载的字典,导致错误。 我通过使用try / except基本上执行该功能两次来解决这个问题。
我的代码:
import mMyUtils
import pickle
import dictionaryGenerator
LOOK_UP = 1
ADD = 2
CHANGE = 3
DELETE = 4
QUIT = 5
def main():
hasError = False
try:
emails = pickle.load(open('emails.dat', 'rb'))
choice = 0
while choice != QUIT:
choice = getMenuChoice()
if choice == LOOK_UP:
lookUp(emails)
elif choice == ADD:
add(emails)
elif choice == CHANGE:
change(emails)
elif choice == DELETE:
delete(emails)
else:
print("Good-bye!")
exit
except Exception as err:
hasError = True
mMyUtils.printError("Error: no such file",err)
mMyUtils.writeToErrorLog()
finally:
if hasError:
emails = {}
choice = 0
while choice != QUIT:
choice = getMenuChoice()
if choice == LOOK_UP:
lookUp(emails)
elif choice == ADD:
add(emails)
elif choice == CHANGE:
change(emails)
elif choice == DELETE:
delete(emails)
else:
print("Good-bye!")
exit
def getMenuChoice():
print()
print('Name and Email Address Catalog')
print('------------------------------')
print('1. Look up an email address')
print('2. Add a new email address')
print('3. Change an email address')
print('4. Delete an email address')
print('5. Quit the program')
print()
choice = int(input('Enter the choice: '))
while choice < LOOK_UP or choice > QUIT:
choice = int(input('Enter a valid choice: '))
return choice
def lookUp(emails):
name = input('Enter a name: ')
print(emails.get(name, 'Not found.'))
def add(emails):
name = input('Enter a name: ')
address = input('Enter an email address: ')
if name not in emails:
emails[name] = address
with open("emails.dat", "wb") as infile:
pickle.dump(emails, infile)
else:
print('That entry already exists.')
def change(emails):
name = input('Enter a name: ')
if name in emails:
address = input('Enter the new address: ')
emails[name] = address
with open("emails.dat", "wb") as infile:
pickle.dump(emails, infile)
else:
print('That name is not found.')
def delete(emails):
name = input('Enter a name: ')
if name in emails:
del emails[name]
else:
print('That name is not found.')
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.