Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

python - Unable to encode/decode pprint output

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, like my api.py

As I mention on the related question, I use HttpResponse to return the api documentation. Since I defined encoding by:

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

Everything is ok, and when I call my API service, there are no encoding problems except the string formed from a dictionary by pprint

Since I am using Turkish characters in some values in my dict, pprint converts them to unichr equivalents, like:

API_STATUS = {
    1: 'mü?teri',
    2: 'some other status message'
}

my_str = 'Here is the documentation part that contains Turkish chars like i?ü???'
my_str += pprint.pformat(API_STATUS, indent=4, width=1)
return HttpRespopnse(my_str, content_type='text/plain; charset=utf-8')

And my plain text output is like:

Here is the documentation part that contains Turkish chars like i?ü???

{
    1: 'mxc3xbcxc5x9fteri',
    2: 'some other status message'
}

I try to decode or encode pprint output to different encodings, with no success... What is the best practice to overcome this problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'iu015fxfcu011fxe7xf6'}
MyPrettyPrinter().pprint(d)   # {'foo': i?ü???}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...