【问题标题】:Python 3.3 dump and load pickled dictionaryPython 3.3 转储和加载腌制字典
【发布时间】:2016-03-15 01:33:57
【问题描述】:

我正在学习 Tony Gaddis 的“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()

【问题讨论】:

    标签: python dictionary serialization python-3.3 jsonpickle


    【解决方案1】:

    如果您使用open("my_file","r") 打开文件进行读取,则不会更改该文件。该文件必须已经存在。如果您使用open("my_file","w") 打开一个文件进行写入,它将创建一个新文件,如果旧文件存在,则覆盖它。第一种形式(阅读)是默认形式,因此您可以根据需要省略第二个 "r" 参数。这在 Python 标准库文档中有记录。

    【讨论】:

    • 感谢您的回复。我阅读了文档,但仍然没有澄清我的问题。假设,我最初打开文件创建一个空文件并向该文件添加一些数据,然后使用二进制流转储它。由于我以后需要将二进制数据附加到同一个文件中,我还是应该以“r”模式打开它还是应该以“rb”模式打开它?
    • 感谢 Paul Cornelius 的回复。我阅读了文档,但仍然没有澄清我的问题。假设,我最初打开文件创建一个空文件并向该文件添加一些数据,然后使用二进制流转储它。由于将来我需要将二进制数据附加到同一个文件中,我还是应该以“r”模式打开它还是应该以“rb”模式打开它?第 9 章提到以二进制模式写入,但我假设这是针对新文件的。第 6 章提到了附加到打开文件的“a”模式。 “a”模式可以与二进制写入结合使用吗?再次感谢。
    • 如果您需要写入二进制数据,请将“b”附加到文件模式字符。我不知道您为什么认为它仅适用于新文件。它不是。如果您需要附加到已经存在的文件,则以“a”模式打开它。 “ab”表示附加到二进制文件。我对您的陈述“向该文件添加一些数据并使用二进制流转储它”感到困惑,因为它们是同一回事。文件只能做两件事:读取和写入。追加就像写入一样,但您从现有文件的末尾开始写入,而不是从头开始。
    【解决方案2】:

    使用 open("myfile", 'r+') 这允许读取和写入功能。 (至少在 2.7 中)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 2013-09-02
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多