Yunshi
Yunshi

Reputation: 43

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 1206: ordinal not in range(128)

I have an array W containing float numbers.

W.dtype = float32 
type(W) = <type 'numpy.ndarray'>

Then I pickle.dump() it into a mr.pkl file,

pickle.dump(W, open("/home/mr.pkl", "wb"))

but when I load it,

pickle.load(open("/home/mr.pkl","rb"))

an error occurs:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 1206: ordinal not in range(128)

I don't know why, I was confused about it for a week, can any one help me about this? any help is appreciated, thank you a lot!

Upvotes: 2

Views: 7298

Answers (2)

october
october

Reputation: 3

OK, I find out a solution but only in console mode.
The steps are as followings.

  1. Why and Can not import mnist_loader? Yes, you should put your 'yourscript.py' in the same folder with mnist_loader.py
  2. Why an error 'ascii' codec can't decode byte 0x90 in position 614: ordinal not in range(128)'? Yes, 'yourscript.py' need in the same folder with mnist_loader.py >> Like me if you use IPython, you need download yourscript.ipynb as 'yourscript.py'
  3. If uses P3.X, please rewrite something in mnist_loader.py as followings " import _pickle as Pickle" and "Pickle.load(f, encoding='latin1')"
  4. When you try to print or get data from Pickle.load(...), please transfer 'zip' type from output of Pickle.load(...) as a list type like list(train_data), ...
  5. In console mode, ...in mnist_loader.py folder(in general ..\src)...>> Python yourscript.py
  6. You can see it is workable. For example: print('len ', len(list(train_data))), it shows 50000.

But...

7. I still do not know why it is not workable in IPython.

Upvotes: 0

Linda MacPhee-Cobb
Linda MacPhee-Cobb

Reputation: 7856

I had the same problem, this code worked for me. The encoding='latin1' is the important part.

# read in data from pickle file created with Load_Data.py
pickle_file = 'mnist.pkl'
with open(pickle_file, 'rb') as f:
    train_set, valid_set, test_set = pickle.load(f, encoding='latin1')

Upvotes: 8

Related Questions