用于读/写的Python 3.3 Pickle错误

时间:2013-12-04 19:31:43

标签: python python-3.x pickle

我正在尝试编写一个显示菜单的程序,允许用户写入文件,从文件中读取或退出。该文件包含一个列表对象,所以我使用的是.dat文件。我已经在这个网站上阅读了python文档和'pickle error'线程的负载,但似乎无法理解为什么我得到了我得到的错误。我喜欢任何见解!

write_to_file功能错误:

integer is required

据我所知,我正在使用open的正确形式,这似乎是给其他用户带来此错误的麻烦,我在Python文档中找不到任何关于pickle.dump的必需整数参数(另外,我很确定我用来允许用户输入数据到文件的方法不正确,但我无法解决pickle错误在它之前。)

def write_to_file():
    s = open('studentInfo.dat')
    pickle.dump(info, s, 'wb')
    shelve.open(s)
    print(s)
    print("You may now add information to the file:")
    input(s[''])
    s.close()

read_file功能错误:

io.UnsupportedOperation: write

我在这个函数中没有'w''wb'个参数,我希望它仍然是一个只读的动作。写错误隐藏在哪里?

def read_file():
    f = open('studentInfo.dat', 'rb')
    pickle.dump(info, f)
    shelve.open(f, 'rb')
    print("Here is the student information: \n")
    print(f)
    f.close()

这是完整的代码:

#import necessary modules:
import pickle, shelve

# create list object
info = [[("student", "John"),("GPA","4.0"), ("ID", "01234")],
        [("student", "Harry"),("GPA","3.2"), ("ID", "03456")],
        [("student", "Melissa"),("GPA","1.8"), ("ID", "05678")],
        [("student", "Mary"),("GPA","3.5"), ("ID", "07899")]]

#Function Definitions
def write_to_file():
    s = open('studentInfo.dat')
    pickle.dump(info, s, 'wb')
    shelve.open(s)
    print(s)
    print("You may now add information to the file:")
    input(s[''])
    s.close()

def read_file():
    f = open('studentInfo.dat', 'rb')
    pickle.dump(info, f)
    shelve.open(f, 'rb')
    print("Here is the student information: \n")
    print(f)
    f.close()

#def main(): #while loop as program engine, constantly prompt user, display menu, etc.

menu = ("\n0 - Exit the Program",               #Exit
        "\n1 - Add student information",        #Write to file
        "\n2 - Print student information")  #Read file

print(menu)
menuchoice = int(input("Please enter a number that matches the menu option you want: "))
##writetofile = open("studentInfo.dat", "wb")
##printinfo = open("studentInfo.dat", "rb")

if menuchoice == 0:
    input("\nPress the 'enter' key to exit the program.")
elif menuchoice == 1:
    print("You may add a student, gpa, or student ID to the file")
    write_to_file()
elif menuchoice == 2:
    read_file()

1 个答案:

答案 0 :(得分:4)

您需要将mode参数传递给open()来电,将而不是传递给pickle.dump()

s = open('studentInfo.dat', 'wb')
pickle.dump(info, s)

要从打开的文件加载,请使用pickle.load()

f = open('studentInfo.dat', 'rb')
info = pickle.load(f)

您根本不需要shelve模块并在此处致电。删除那些。

您可能希望将文件用作上下文管理器,自动关闭它们:

with open('studentInfo.dat', 'wb') as outputfile:
    pickle.dump(info, outputfile)

with open('studentInfo.dat', 'rb') as inputfile:
    info = pickle.load(inputfile)

打开后,您无法添加只是将非结构化的附加信息添加到文件中;在挑选info之前将新信息添加到info

def write_to_file():
    # take input and add that to `info` here.
    # gather a name, GPA and ID into `new_name`, `new_gpa` and `new_id`
    info.append([("student", new_name),("GPA", new_gpa), ("ID", new_id)])

    with open('studentInfo.dat', 'wb') as outputfile:
        pickle.dump(info, outputfile)

您的read_file()函数可能应该返回读取信息,您应该info明确global

def read_file():
    with open('studentInfo.dat', 'rb') as inputfile:
        info = pickle.load(inputfile)
    return info

通过从函数返回,您可以将其分配回info或打印它:

read_info = read_file()
print("Here is the student information: \n")
print(read_info)