Unable to encode/decode pprint output(无法编码/解码 pprint 输出)
问题描述
这个问题是基于那个的副作用.
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şüğçö}
这篇关于无法编码/解码 pprint 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法编码/解码 pprint 输出
- 沿轴计算直方图 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何将一个类的函数分成多个文件? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
