Skip to content

Instantly share code, notes, and snippets.

@priancho
Created April 25, 2017 02:30
Show Gist options
  • Save priancho/a2a45be370d24bc861ac0e806300a2aa to your computer and use it in GitHub Desktop.
Save priancho/a2a45be370d24bc861ac0e806300a2aa to your computer and use it in GitHub Desktop.
python 2.x pprint with UTF-8 text
import pprint
class MyPrettyPrinter(pprint.PrettyPrinter):
"""PrettyPrint with UTF-8 text.
Refer to: http://stackoverflow.com/questions/10883399/unable-to-encode-decode-pprint-output
"""
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment