简体   繁体   English

无法对pprint输出进行编码/解码

[英]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 我的.py文件都具有# -*- coding: utf-8 -*-第一行是# -*- coding: utf-8 -*-编码定义器,例如api.py

As I mention on the related question, I use HttpResponse to return the api documentation. 正如我在相关问题上提到的那样,我使用HttpResponse返回api文档。 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 一切正常,当我调用API服务时,除了通过pprint由字典形成的字符串没有任何编码问题

Since I am using Turkish characters in some values in my dict, pprint converts them to unichr equivalents, like: 由于我在字典中的某些值中使用土耳其语字符,因此pprint将其转换为等同的unichr ,例如:

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: 'm\xc3\xbc\xc5\x9fteri',
    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 我尝试将pprint输出解码或编码为不同的编码,但没有成功...解决此问题的最佳实践是什么

pprint appears to use repr by default, you can work around this by overriding PrettyPrinter.format : pprint默认情况下似乎使用repr ,您可以通过重写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şüğçö}

You should use unicode strings instead of 8-bit ones: 您应该使用unicode字符串而不是8位字符串:

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. pprint模块旨在以可读的方式打印出所有可能的嵌套结构。 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. 为此,它将打印对象表示形式,而不是将其转换为字符串,因此无论是否使用unicode字符串,您都将以转义语法结束。 But if you're using unicode in your document, then you really should be using unicode literals! 但是,如果您在文档中使用unicode,那么您确实应该使用unicode文字!

Anyway, thg435 has given you a solution how to change this behaviour of pformat. 无论如何, thg435为您提供了一个解决方案 ,该方案如何更改pformat的这种行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM