python 中的酸洗问题 - 程序退出后项目不会保存到文件
Issues with pickling in python - items not saving to file once program exits
我正在尝试创建一个 python 程序来保存我朋友的生日并轻松访问它们并每天检查生日(我不擅长记住日期而且我从不使用 facebook),但是当我添加了一个新的生日,它只能在我结束程序之前访问 - 然后它再次消失。我已经为此苦苦挣扎了一段时间,非常感谢帮助修复错误。谢谢!
import time
import pickle
def main():
birthday_file = open('birthdays_dict.dat','ab')
birthday_doc = open('birthdays_dict.dat','rb')
birthdays = pickle.load(birthday_doc)
date = time.strftime("%m/%d")
again = 'y'
while again.lower() == 'y' or again.lower() == 'yes':
choice = menu_choice()
if choice == 1:
name = add_name()
birthday = add_birthday()
birthdays[name] = birthday
print(name)
print(birthday)
pickle.dump(birthdays,birthday_file)
elif choice == 2:
print('Birthdays today(' + date + '):')
birth_today = {}
for key, value in birthdays.items():
if value == date:
print(key)
elif choice == 3:
search_name = input('Enter name to search: ')
print()
if search_name in birthdays:
print(birthdays[search_name])
if birthdays[search_name] == date:
print('Their birthday is today!')
else:
print('Not found')
else:
print('Not a valid selection!')
print()
again = go_again()
birthday_file.close()
birthday_doc.close()
你的问题是你一直在文件中添加新的指令而不是替换旧的,但是在启动时你只加载第一个而不是所有的。
要解决此问题,您需要更改:
birthday_file = open('birthdays_dict.dat','ab')
… 对此:
birthday_file = open('birthdays_dict.dat','wb')
但不要单独进行更改,因为那样会在您阅读旧版本之前删除文件!
你可能想在函数的顶部做这样的事情:
with open('birthdays_dict.dat', 'rb') as birthday_doc:
birthdays = pickle.load(birthday_doc)
我使用了 with
语句,所以文件会在 load
之后自动关闭,所以我们稍后覆盖它绝对安全。
然后以后,当你想写文件的时候,就是在w
模式下打开它,擦除文件并用新版本覆盖它——此时你还不如关闭它立即,因为如果你再次写入它,你会想先再次擦除它,所以让我们再次使用 with
:
with open('birthdays_dict.dat', 'wb') as birthday_doc:
pickle.dump(birthdays, birthday_doc)
我正在尝试创建一个 python 程序来保存我朋友的生日并轻松访问它们并每天检查生日(我不擅长记住日期而且我从不使用 facebook),但是当我添加了一个新的生日,它只能在我结束程序之前访问 - 然后它再次消失。我已经为此苦苦挣扎了一段时间,非常感谢帮助修复错误。谢谢!
import time
import pickle
def main():
birthday_file = open('birthdays_dict.dat','ab')
birthday_doc = open('birthdays_dict.dat','rb')
birthdays = pickle.load(birthday_doc)
date = time.strftime("%m/%d")
again = 'y'
while again.lower() == 'y' or again.lower() == 'yes':
choice = menu_choice()
if choice == 1:
name = add_name()
birthday = add_birthday()
birthdays[name] = birthday
print(name)
print(birthday)
pickle.dump(birthdays,birthday_file)
elif choice == 2:
print('Birthdays today(' + date + '):')
birth_today = {}
for key, value in birthdays.items():
if value == date:
print(key)
elif choice == 3:
search_name = input('Enter name to search: ')
print()
if search_name in birthdays:
print(birthdays[search_name])
if birthdays[search_name] == date:
print('Their birthday is today!')
else:
print('Not found')
else:
print('Not a valid selection!')
print()
again = go_again()
birthday_file.close()
birthday_doc.close()
你的问题是你一直在文件中添加新的指令而不是替换旧的,但是在启动时你只加载第一个而不是所有的。
要解决此问题,您需要更改:
birthday_file = open('birthdays_dict.dat','ab')
… 对此:
birthday_file = open('birthdays_dict.dat','wb')
但不要单独进行更改,因为那样会在您阅读旧版本之前删除文件!
你可能想在函数的顶部做这样的事情:
with open('birthdays_dict.dat', 'rb') as birthday_doc:
birthdays = pickle.load(birthday_doc)
我使用了 with
语句,所以文件会在 load
之后自动关闭,所以我们稍后覆盖它绝对安全。
然后以后,当你想写文件的时候,就是在w
模式下打开它,擦除文件并用新版本覆盖它——此时你还不如关闭它立即,因为如果你再次写入它,你会想先再次擦除它,所以让我们再次使用 with
:
with open('birthdays_dict.dat', 'wb') as birthday_doc:
pickle.dump(birthdays, birthday_doc)