Problems with pickle and encodings
I am working with sizeable set of text files. Many of them written in different encodings. I create list of objects which contains some of the substrings from those text files. I am taking care 开发者_StackOverflowof encoding problems when opening the files (objects are created correctly and could be used). Here is my list:
len(hands)
47580
type(hands)
<class 'list'>
type(hands[0])
<class '__main__.BridgeHand'>
Now I try to pickle this object:
import pickle
pickle.dump(hands, open("handspi.p", "wb"))
It creates 9MB file handspi. The problems start when I try to unpickle it:
hh = pickle.load(open(#some path to this pickle file))
Stack trace ends with:
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 do I do about it ? Thanks for help :)
You might want to try opening the file in binary mode as you are currently reading it as an ascii file.
open('picklefile.pkl','rb')
精彩评论