Python Serialize Object And Decode Return An Invalid Start Byte Error
I would to serialize my object ancd encode() decode() it for manage byte/string format. I try creating this class: class test(object): def __init__(self,uid): self.uid.
Solution 1:
pickle.dumps
creates a byte array object (denoted by b'...'
) which do not have to be a valid Unicode string therefore decode
method on such a byte array can fail as it failed for you.
In general, the byte array from pickle.dumps
should not ever be interpreted as string. It is an internal representation of pickle
module and there is no guarantee to be a valid string in any encoding (and a Unicode one in particular).
If your intention is to transform the picked object back to an original object then use pickle.loads
method on the result from pickle.dumps
. You can of course write it to a file / read it to a file in between (as is the typical use case).
Post a Comment for "Python Serialize Object And Decode Return An Invalid Start Byte Error"