title: How to pickle unicodes and save them in utf-8 databases
body: <p>I have a database (mysql) where I want to store pickled data.</p>
  
  <p>The data can be for instance a dictionary, which may contain unicode, e.g.</p>
  
  <pre><code>data = {1 : u'é'}
  </code></pre>
  
  <p>and the database (mysql) is in utf-8.</p>
  
  <p>When I pickle,</p>
  
  <pre><code>import pickle
  pickled_data = pickle.dumps(data)
  print type(pickled_data) # returns &lt;type 'str'&gt;
  </code></pre>
  
  <p>the resulting pickled_data is a string.</p>
  
  <p>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</p>
  
  <pre><code>UnicodeDecodeError "'utf8' codec can't decode byte 0xe9 in position X"
  </code></pre>
  
  <p>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?</p>
  
  <p>I see two possible candidates:</p>
  
  <ol>
  <li><p>Encode the result of the pickle.dump to utf-8 and store it. When I want to pickle.load, I have to decode it.</p></li>
  <li><p>Store the pickled string in binary format (how?), which forces all characters to be within ascii.</p></li>
  </ol>
  
  <p>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>
  
  <p>(P.S. This is for instance useful in <a href="https://www.djangoproject.com/" rel="noreferrer">Django</a>)</p>