python读取 IEMOCAP_text_context.pickle 文件时报错的解决办法

当Python在加载IEMOCAP_text_context.pickle文件时遇到asciicodec错误,可以通过指定latin1编码来解码。使用`pickle.load(handle,encoding=latin1)`可以成功读取文件内容,避免ordinalnotinrange(128)的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python读取 IEMOCAP_text_context.pickle 文件时报错的解决办法


python在加载 IEMOCAP_text_context.pickle 文件时报

'ascii' codec can't decode byte 0xd2 in position 33: ordinal not in range(128)

的错误,原因是文件的编码方式与读取的编码方式不一致。


解决办法:

with open('./IEMOCAP_text_context.pickle', 'rb') as handle:
    data = pickle.load(handle, encoding='latin1')
print( list(data.keys())[:20] )

打印内容:
['Ses01F_script02_2_F042', 'Ses01F_script02_2_F043', 'Ses01F_script02_2_F040', 'Ses01F_script02_2_F041', 'Ses01F_script02_2_F046', 'Ses01F_script02_2_F044', 'Ses01F_script02_2_F045', 'Ses05F_script01_1_M010', 'Ses05F_script01_1_M011', 'Ses05F_script01_1_M012', 'Ses05F_script01_1_M013', 'Ses05F_script01_1_M014', 'Ses05F_script01_1_M015', 'Ses05F_script01_1_M016', 'Ses05F_script01_1_M017', 'Ses05M_script02_2_F030', 'Ses05M_script02_2_F031', 'Ses05M_script02_2_F032', 'Ses05M_script02_2_F034', 'Ses05M_script02_2_F037']
Traceback (most recent call last): File "C:\Users\唐添美.DESKTOP-95B881R\Desktop\MNIST实现\test.py", line 38, in <module> mynet = torch.load('MNIST_4_acc_0.9692999720573425.pth', map_location=torch.device('cpu')) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\唐添美.DESKTOP-95B881R\.conda\envs\python高级应用\Lib\site-packages\torch\serialization.py", line 1524, in load raise pickle.UnpicklingError(_get_wo_message(str(e))) from None _pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint. (1) In PyTorch 2.6, we changed the default value of the `weights_only` argument in `torch.load` from `False` to `True`. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source. (2) Alternatively, to load with `weights_only=True` please check the recommended steps in the following error message. WeightsUnpickler error: Unsupported global: GLOBAL __main__.MyNet was not an allowed global by default. Please use `torch.serialization.add_safe_globals([__main__.MyNet])` or the `torch.serialization.safe_globals([__main__.MyNet])` context manager to allowlist this global if you trust this class/function. Check the documentation of torch.load to learn more about types accepted by default with weights_only https://pytorch.org/docs/stable/generated/torch.load.html.
最新发布
05-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值