gpt4 book ai didi

python - 如何使用 Windows 操作系统从 .pkl 文件获取数据

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:36 30 4
gpt4 key购买 nike

我有一个 .pkl 文件,它可以在我的 MAC 操作系统中完美加载,但无法在 Windows 计算机中加载。我在 anaconda 上使用 python 3。这是我的代码:

data=pickle.load(open("ydata1.pkl",'rb'))

错误:UnicodeDecodeError:“ascii”编解码器无法解码位置 2295 中的字节 0xc3:序数不在范围内(128)

所以我尝试了这个:

data=pickle.load(open("ydata1.pkl",'r'))

但我收到一条错误消息:需要类似字节的对象,而不是“str”

谁能告诉我哪里出错了?

最佳答案

在 rb 模式下使用 open():

import pickle

with open('ydata1.pkl', 'rb') as p_f:
data = pickle.load(p_f)

取自文档:https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

还要确保您没有 Python 2/Python 3 pickle 兼容性问题:Pickle incompatibility of numpy arrays between Python 2 and 3

关于python - 如何使用 Windows 操作系统从 .pkl 文件获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47339602/

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