Python 3.3读取/写入的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()

您需要将模式参数传递给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)

最新更新