如何解决使用 from pickle.load() 函数时出现的错误?
问题描述:
我想在 IPython 环境中加载一个数据集并使用它.
I would like to load a dataset in IPython Environment and and use it.
在包含数据集的目录中,我有这些文件:
In the directory containing the dataset, I've got these files:
- batches.meta
- data_batch_1
- data_batch_2
- data_batch_3
- data_batch_4
- data_batch_5
- 自述
- test_batch
我写了这段代码:
import os
import pickle as pickle
import numpy as np
import matplotlib.pyplot as plt
#Function Definition
def load_CIFAR(ROOT):
xs=[];
ys=[];
for b in range(6):
f = os.path.join(ROOT, "data_batch_%d"%(b+1));
X, Y = load_CIFAR_batch(f);
xs.append(X);
ys.append(Y);
Xtr = np.concatenate(xs);
Ytr = np.concatenate(ys);
del X, Y;
Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, "test_batch"));
return Xtr, Ytr, Xte, Yte
#Function Definition
def load_CIFAR_batch(filename):
with open(filename, 'r') as f:
****** Here is where error occurs
datadict = pickle.load(f);
******
X = datadict['data'];
Y = datadict['labels'];
X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float");
Y = np.array(Y);
return X, Y;
但是,当我使用这个函数通过以下命令加载这个数据集时,我遇到了 [a bytes-like object is required, not 'str'] 错误.
But, when I used this function for loading this dataset with following command, I came across with [a bytes-like object is required, not 'str'] error.
#The directory of my dataset in my hard drive
url = 'D:\\OTIWU\\data\\cifar10'
Xtr, Ytr, Xte, Yte = load_CIFAR(url)
上面是我用过的命令.
The whole error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-f0576df4fbda> in <module>()
----> 1 Xtr, Ytr, Xte, Yte = load_CIFAR(url)
<ipython-input-10-fedf6bd7c144> in load_CIFAR(ROOT)
4 for b in range(1,6):
5 f=os.path.join(ROOT, "data_batch_%d" % (b, ));
----> 6 X, Y=load_CIFAR_batch(f);
7 xs.append(X);
8 ys.append(Y);
<ipython-input-13-368cd3e9d8d2> in load_CIFAR_batch(filename)
1 def load_CIFAR_batch(filename):
2 with open(filename, 'r') as f:
----> 3 datadict = pickle.load(f);
4
5 X = datadict['data'];
TypeError: a bytes-like object is required, not 'str'
我该如何解决这样的问题?
How can I solve such problem?
答
您需要以二进制模式打开您的文件:
You need to open your file in binary mode:
with open(filename, 'rb') as f: