2

My code is:

import _pickle
with open('items_10000_matrix.pickle', 'rb') as f:
    data_new = _pickle.load(f)

But an error occurs:

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

I am using windows 10 + python 3.5 with VS tools for python.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    You say doing an `import pickle` fails - how exactly does it fail? You shouldn't be/need to be using `_pickle`... – Jon Clements Aug 27 '16 at 17:58
  • @NinjaPuppy Sorry, it doesnt fail any more, that were just my previus manipulations... –  Aug 27 '16 at 18:01
  • @martineau Sorry, but: **a bytes-like object is required, not 'str'** –  Aug 27 '16 at 18:11
  • @NinjaPuppy I'am sorry, but the problem remains to be, sir. –  Aug 27 '16 at 18:16
  • 2
    OK, try using `_pickle.load(f, encoding='bytes')`. – martineau Aug 27 '16 at 18:22
  • @martineau: You should never need to open in text mode when reading pickles. Opening `"rb"` is *the* correct way to open a file you'll be unpickling from. I want to know how the original data was pickled here; if it's a legal pickle, written correctly, `open`ing `"rb"` and calling `pickle.load` (`_pickle` is the accelerator `pickle` wraps; never use it directly) should work. – ShadowRanger Aug 27 '16 at 18:29
  • @martineau It seems to end with no errors. But how am i supposed to work with that file - just like as i have encoded it from ASCII? I am asking, because iam not able to print it. –  Aug 27 '16 at 18:29
  • @Vladislav: Not sure what you mean by "work with that file" — it's a pickle file and now you have successfully opened and deserialized the object(s) in it (and automatically closed it via the `with`). What more do you want to do with it, `print(data_new)` perhaps? – martineau Aug 27 '16 at 18:33
  • @ShadowRanger: My mistake, I was thinking of csv files in Python 3. – martineau Aug 27 '16 at 18:34
  • @martineau Is that so? When i open this file, i can see, that it hadn't changed at all. Sorry, if i cant understand something, but working with othe f.formats was quite easy, opposite to this one. –  Aug 27 '16 at 18:39
  • 1
    @Vladislav: The only thing a pickle file is really good for is storing pickled data that can be read back later. You can store multiple items in one, but unless they are all within some sort of container object, such as a list or dictionary, you will have to unpickle (load) each one separately. See this [answer of mine](http://stackoverflow.com/questions/4529815/saving-an-object-data-persistence-in-python/4529901#4529901) for some examples. – martineau Aug 27 '16 at 18:48
  • Okay, thank you. Could you, please, place your answer, so i can mark it –  Aug 27 '16 at 18:50

2 Answers2

0

Try using _pickle.load(f, encoding='bytes').

By the way, in Python 3, there's no reason to explicitly import _pickle rather than pickle because it will automatically switch to the C version if it's available. See the accepted answer to the question What difference between pickle and _pickle in python 3?

Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Vladislav: I feel un-accepting my answer was a little unfair. I might have suggested switching to Python 2 if you hadn't explicitly stated you were using python 3.5 (and tagged your question Python-3.x). It's also fairly unusual for someone to be able to easily switch from one to other just like that. – martineau Oct 28 '16 at 00:06
0

I was told to use python 2 instead of python 3, and it worked. Still dont know the solution for python3