python pickle load_Python-pickle.load()需要一个位置参数(给定2个)

This produces an error:

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

Here is my code:

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

Why isn't it pickling data?

解决方案

This is not how you use pickle.load:

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

It takes a file object as an argument when de-serializing from a file, not strings.

Try this instead:

# create file object with permissions

with open('phonebook.json', 'r') as f:

# load using pickle de-serializer

phoneBook_Ori = pickle.load(f)

Saving is almost the same, make sure you have the updated phonebook in scope:

with open('phonebook.json', 'wb') as f:

phoneBook_Ori = pickle.dump(phonebook, f)

As for the rest of the code, you may want to read another answer I've given that is very similar.

UnpicklingError Traceback (most recent call last) Input In [66], in <cell line: 36>() 30 Kcat_model = model.KcatPrediction(device, n_fingerprint, n_word, 2*dim, layer_gnn, window, layer_cnn, layer_output).to(device) 31 ##‘KcatPrediction’是一个自定义模型类,根据给定参数初始化一个Kcat预测模型。使用了上述参数,如果要进行调参在此处进行 32 # directory_path = '../../Results/output/all--radius2--ngram3--dim20--layer_gnn3--window11--layer_cnn3--layer_output3--lr1e-3--lr_decay0/archive/data' 33 # file_list = os.listdir(directory_path) 34 # for file_name in file_list: 35 # file_path = os.path.join(directory_path,file_name) ---> 36 Kcat_model.load_state_dict(torch.load('MAEs--all--radius2--ngram3--dim20--layer_gnn3--window11--layer_cnn3--layer_output3--lr1e-3--lr_decay0.5--decay_interval10--weight_decay1e-6--iteration50.txt', map_location=device)) 37 ##表示把预训练的模型参数加载到Kcat_model里,‘torch.load’表示函数用于文件中加载模型参数的状态字典(state_dict),括号内表示预训练参数文件位置 38 predictor = Predictor(Kcat_model) File ~/anaconda3/lib/python3.9/site-packages/torch/serialization.py:815, in load(f, map_location, pickle_module, weights_only, **pickle_load_args) 813 except RuntimeError as e: 814 raise pickle.UnpicklingError(UNSAFE_MESSAGE + str(e)) from None --> 815 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args) File ~/anaconda3/lib/python3.9/site-packages/torch/serialization.py:1033, in _legacy_load(f, map_location, pickle_module, **pickle_load_args) 1027 if not hasattr(f, 'readinto') and (3, 8, 0) <= sys.version_info < (3, 8, 2): 1028 raise RuntimeError( 1029 "torch.load does not work with file-like objects that do not implement readinto on Python 3.8.0 and 3.8.1. " 1030 f"Received object of type "{type(f)}". Please update to Python 3.8.2 or newer to restore this " 1031 "functionality.") -> 1033 magic_number = pickle_module.load(f, **pickle_load_args) 1034 if magic_number != MAGIC_NUMBER: 1035 raise RuntimeError("Invalid magic number; corrupt file?") UnpicklingError: invalid load key, 'E'. 这个问题怎么解决
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值