title: What is the right way to compress and decompress UTF-8 data using zlib?
body: <p>I have a very long JSON message that contains characters that go beyond the ASCII table. I convert it into a string as follows:</p>
  
  <pre><code>messStr = json.dumps(message,encoding='utf-8', ensure_ascii=False, sort_keys=True)
  </code></pre>
  
  <p>I need to store this string using a service that restricts its size to X bytes. I want to split the JSON string into pieces of length X and store them separately. I ran into some issues doing this (described <a href="https://stackoverflow.com/questions/18364594/why-am-i-getting-no-json-object-could-be-decoded-when-batch-writing-items-into">here</a>) so I want to compress the string slices to work around those issues. I tried to do this:</p>
  
  <pre><code>ss = mStr[start:fin]    # get piece of length X
  ssc = zlib.compress(ss) # compress it
  </code></pre>
  
  <p>When I do that, I get the following error from <code>zlib.compress</code>:</p>
  
  <pre><code>UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 225: ordinal not in range(128)
  </code></pre>
  
  <p>What is the right way to compress a UTF-8 string and what is then the right way to decompress it?</p>