gpt4 book ai didi

用于读/写的 Python 3.3 Pickle 错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:32 25 4
gpt4 key购买 nike

我正在尝试编写一个显示菜单并允许用户写入文件、从文件读取或退出的程序。该文件包含一个列表对象,因此我使用的是 .dat 文件。我已经阅读了本网站上的 python 文档和大量“pickle error”线程,但似乎无法理解为什么我会收到这些错误。我希望有任何见解!

write_to_file 函数错误:

integer is required

据我所知,我使用的是 open 的正确形式,这似乎是导致其他用户遇到此错误的原因,而且我在关于 pickle.dump 所需整数参数的 Python 文档(此外,我很确定我用来允许用户向文件输入数据的方法不正确,但我没有能够克服之前的 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 before pickling 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 显式全局:

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)

关于用于读/写的 Python 3.3 Pickle 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20384232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com