I have a database (mysql) where I want to store pickled data.
The data can be for instance a dictionary, which may contain unicode, e.g.
data = {1 : u'é'}
and the database (mysql) is in utf-8.
When I pickle,
import pickle
pickled_data = pickle.dumps(data)
print type(pickled_data) # returns <type 'str'>
the resulting pickled_data is a string.
When I try to store this in a database (e.g. in a Textfield) this can causes problems. In particular, I'm getting at some point a
UnicodeDecodeError "'utf8' codec can't decode byte 0xe9 in position X"
when trying to save the pickled_data in the database. This makes sense because pickled_data can have non-utf-8 characters. My question is how do I store pickled_data on a utf-8 database?
I see two possible candidates:
Encode the result of the pickle.dump to utf-8 and store it. When I want to pickle.load, I have to decode it.
Store the pickled string in binary format (how?), which forces all characters to be within ascii.
My issue is that I'm not seeing what are the consequences of choosing one of this options in the long run. Since the change already requires some effort, I'm driven to ask for an opinion on this issue, asking for eventual better candidates.
(P.S. This is for instance useful in Django)
Pickle data is opaque, binary data, even when you use protocol version 0:
When you try to store that in a
TextField
, Django will try to decode that data to UTF8 to store it; this is what fails because this is not UTF-8 encoded data; it is binary data instead:The solution is to not try to store this in a
TextField
. Use aBinaryField
instead:You have a
bytes
value (Python 2 strings are byte strings, renamed tobytes
in Python 3).If you insist on storing the data in a text field, explicitly decode it as
latin1
; the Latin 1 codec maps bytes one-on-one to Unicode codepoints:and make sure you encode it again before unpickling again:
Do note that if you let this value go to the browser and back again in a text field, the browser is likely to have replaced characters in that data. Internet Explorer will replace
\n
characters with\r\n
, for example, because it assumes it is dealing with text.Not that you ever should allow accepting pickle data from a network connection in any case, because that is a security hole waiting for exploitation.