python - Unable to encode/decode pprint output -


this question based on side-effect of that one.

my .py files have # -*- coding: utf-8 -*- encoding definer on first line, api.py

as mention on related question, use httpresponse return api documentation. since defined encoding by:

httpresponse(cy_content, content_type='text/plain; charset=utf-8') 

everything ok, , when call api service, there no encoding problems except the string formed dictionary pprint

since using turkish characters in values in dict, pprint converts them unichr equivalents, like:

api_status = {     1: 'müşteri',     2: 'some other status message' }  my_str = 'here documentation part contains turkish chars işüğçö' my_str += pprint.pformat(api_status, indent=4, width=1) return httprespopnse(my_str, content_type='text/plain; charset=utf-8') 

and plain text output like:

here documentation part contains turkish chars işüğçö  {     1: 'm\xc3\xbc\xc5\x9fteri',     2: 'some other status message' } 

i try decode or encode pprint output different encodings, no success... best practice overcome problem

pprint appears use repr default, can work around overriding prettyprinter.format:

# coding=utf8  import pprint  class myprettyprinter(pprint.prettyprinter):     def format(self, object, context, maxlevels, level):         if isinstance(object, unicode):             return (object.encode('utf8'), true, false)         return pprint.prettyprinter.format(self, object, context, maxlevels, level)   d = {'foo': u'işüğçö'}  pprint.pprint(d)              # {'foo': u'i\u015f\xfc\u011f\xe7\xf6'} myprettyprinter().pprint(d)   # {'foo': işüğçö} 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -