gpt4 book ai didi

Python - pickle.load() 采用一个位置参数(给定 2 个)

转载 作者:行者123 更新时间:2023-11-30 23:14:19 24 4
gpt4 key购买 nike

这会产生一个错误:

pickle.load() takes one positional argument (2 given)

这是我的代码:

import pickle, os.path

created = False

phoneBook = {}
name = input("Please enter a name(or press enter to end input): ")
while name != '':
number = input("Please enter number: ")
phoneBook[name] = number
name = input("Please enter a name(or press enter to end input): ")
if name == '':
print("Thank You!")

print("Your phonebook contains the following entries:")
for name, number in phoneBook.items():
print("%s - %s" % (name, number))

while not created:
if not os.path.isfile('phonebook.json'):
phoneBook_Ori = pickle.load('phonebook.json', 'r')
created = True
else:
phoneBook_Ori = pickle.load('phonebook.json', 'w')
phoneBook_Upd = phoneBook_Ori.update(phoneBook)
phoneBook_Ori.write(phoneBook_Upd)

phoneBook_Ori.close

为什么不 pickle 数据?

最佳答案

这不是你使用pickle.load的方式:

phoneBook_Ori = pickle.load('phonebook.json', 'r')

从文件反序列化时,它采用文件对象作为参数,而不是字符串。

试试这个:

# create file object with permissions
with open('phonebook.json', 'r') as f:
# load using pickle de-serializer
phoneBook_Ori = pickle.load(f)

保存几乎相同,请确保您的范围内有更新的电话簿:

with open('phonebook.json', 'wb') as f:
phoneBook_Ori = pickle.dump(phonebook, f)

至于其余代码,您可能需要阅读another answer我已经给出了非常相似的信息。

关于Python - pickle.load() 采用一个位置参数(给定 2 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28812686/

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