我正在学习 Tony Gaddis 的“Starting Out With Python”第 3 版中我之前上过的一堂课中的章节练习。我在第 9 章,练习 8 要求我编写一个程序,该程序在文件关闭时将字典(名称:电子邮件)腌制到文件中,并在打开文件时取消保存数据的文件。我已经阅读了那一章中的每一个字,但我仍然不明白你如何在同一个文件中做到这两点。当您使用 open 函数时,它会创建一个文件,据我了解,该文件是一个没有数据的新文件。我认为这可能是一个排序问题,例如在哪里放置转储和加载代码行,但这也没有意义。逻辑要求您必须先打开文件,然后才能转储到该文件。
如果 'open' 函数创建一个文件对象并将其与文件相关联,并且该函数出现在代码的早期(如在 def main 中),那么每次调用该行时它不会将文件清零?
这不是家庭作业。我已经完成了那门课。我这样做是为了我自己的启迪,并希望有任何有助于我理解它的解释。我已经包含了我对解决方案的尝试,这反映在下面的代码中,并且会继续研究它,直到找到解决方案。我只是想,由于这里的基因库更深,我会为自己节省一些时间和挫败感。非常感谢那些选择回复的人,如果我缺乏任何有助于澄清这个问题的相关数据,请告诉我。
import pickle
#global constants for menu choices
ADDNEW = 1
LOOKUP = 2
CHANGE = 3
DELETE = 4
EXIT = 5
#create the main function
def main():
#open the previously saved file
friends_file = open('friends1.txt', 'rb')
#friends = pickle.load(friends_file)
end_of_file = False
while not end_of_file:
try:
friends = pickle.load(friends_file)
print(friends[name])
except EOFError:
end_of_file = True
friends_file.close()
#initialize variable for user's choice
choice = 0
while choice != EXIT:
choice = get_menu_choice() #get user's menu choice
#process the choice
if choice == LOOKUP:
lookup(friends)
elif choice == ADDNEW:
add(friends)
elif choice == CHANGE:
change(friends)
elif choice == DELETE:
delete(friends)
#menu choice function displays the menu and gets a validated choice from the user
def get_menu_choice():
print()
print('Friends and Their Email Addresses')
print('---------------------------------')
print('1. Add a new email')
print('2. Look up an email')
print('3. Change a email')
print('4. Delete a email')
print('5. Exit the program')
print()
#get the user's choice
choice = int(input('Enter your choice: '))
#validate the choice
while choice < ADDNEW or choice > EXIT:
choice = int(input('Enter a valid choice: '))
#return the user's choice
return choice
#the add function adds a new entry into the dictionary
def add(friends):
#open a file to write to
friends_file = open('friends1.txt', 'wb')
#loop to add data to dictionary
again = 'y'
while again.lower() == 'y':
#get a name and email
name = input('Enter a name: ')
email = input('Enter the email address: ')
#if the name does not exist add it
if name not in friends:
friends[name] = email
else:
print('That entry already exists')
print()
#add more names and emails
again = input('Enter another person? (y/n): ')
#save dictionary to a binary file
pickle.dump(friends, friends1.txt)
friends1.close()
#lookup function looks up a name in the dictionary
def lookup(friends):
#get a name to look up
name = input('Enter a name: ')
#look it up in the dictionary
print(friends.get(name, 'That name was not found.'))
#the change function changes an existing entry in the dictionary
def change(friends):
#get a name to look up
name = input('Enter a name: ')
if name in friends:
#get a new email
email = input('Enter the new email address: ')
#update the entry
friends[name] = email
else:
print('That name is not found.')
#delete an entry from the dictionary
def delete(friends):
#get a name to look up
name = input('Enter a name: ')
#if the name is found delete the entry
if name in friends:
del [name]
else:
print('That name is not found.')
#call the main function
main()