Unable to encode/decode pprint output

后端 未结 2 1819
闹比i
闹比i 2020-12-03 07:38

This question is based on a side-effect of that one.

My .py files are all have # -*- coding: utf-8 -*- encoding definer on the first line,

相关标签:
2条回答
  • 2020-12-03 08:16

    pprint appears to use repr by default, you can work around this by 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şüğçö}
    
    0 讨论(0)
  • 2020-12-03 08:40

    You should use unicode strings instead of 8-bit ones:

    API_STATUS = {
        1: u'müşteri',
        2: u'some other status message'
    }
    
    my_str = u'Here is the documentation part that contains Turkish chars like işüğçö'
    my_str += pprint.pformat(API_STATUS, indent=4, width=1)
    

    The pprint module is designed to print out all possible kind of nested structure in a readable way. To do that it will print the objects representation rather then convert it to a string, so you'll end up with the escape syntax wheather you use unicode strings or not. But if you're using unicode in your document, then you really should be using unicode literals!

    Anyway, thg435 has given you a solution how to change this behaviour of pformat.

    0 讨论(0)
提交回复
热议问题