Problems with brine and encodings

I work with a large set of text files. Many of them are written in different encodings. I am creating a list of objects that contains some substrings from these text files. I deal with coding issues when opening files (objects are created correctly and can be used). Here is my list:

len(hands)
47580
type(hands)
<class 'list'>
type(hands[0])
<class '__main__.BridgeHand'>

Now I am trying to sort this object:

import pickle
pickle.dump(hands, open("handspi.p", "wb"))

It creates a 9MB filepi file. Problems arise when I try to decompose it:

hh = pickle.load(open(#some path to this pickle file))

The stack trace ends:

File "C:\Python31\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 30: character maps to      <undefined>

What should I do? Thanks for the help:)

+3
source share
1 answer

You might want to open the file in binary mode, as you are currently reading it as an ascii file.

open('picklefile.pkl','rb')
+7
source

Source: https://habr.com/ru/post/1792500/


All Articles