无法编码/解码 pprint 输出

2022-01-12 00:00:00 python unicode django formatting

问题描述

这个问题是基于那个的副作用.

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

我的 .py 文件在第一行都有 # -*- coding: utf-8 -*- 编码定义器,就像我的 api.py

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

正如我在相关问题中提到的,我使用 HttpResponse 返回 api 文档.由于我通过以下方式定义编码:

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')

一切正常,当我调用我的 API 服务时,除了 pprint 从字典中形成的字符串之外,没有任何编码问题

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

由于我在我的 dict 的某些值中使用土耳其语字符,pprint 将它们转换为 unichr 等价物,例如:

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'
}

我尝试将 pprint 输出解码或编码为不同的编码,但没有成功......克服这个问题的最佳实践是什么

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


解决方案

pprint 似乎默认使用 repr,您可以通过覆盖 PrettyPrinter.format:

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şüğçö}

相关文章