为什么 Python 3 输出 \xe3,一个额外的字符?
Why does Python 3 output \xe3, an extra char?
为什么Python在
的输出中添加\xe3
>>> b'Transa\xc3\xa7\xc3\xa3o'.decode('utf-8')
'Transaç\xe3o'
预期值为:
'Transação'
关于我的环境的更多信息:
>>> import sys
>>> print (sys.version)
3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)]
>>> sys.stdout.encoding
'cp437'
这是在 Console 2 + Powershell 下。
您需要使用支持所有 您要打印的字符的控制台或终端。
在交互式控制台中打印时,字符被编码为适合您的控制台的正确编解码器,使用 backslashreplace
error handler to keep the output readable rather than throw an exception. This is a feature of the default sys.displayhook()
function:
不支持的任何字符
If repr(value)
is not encodable to sys.stdout.encoding
with sys.stdout.errors
error handler (which is probably 'strict'
), encode it to sys.stdout.encoding
with 'backslashreplace'
error handler.
您的控制台可以处理 ç
但不能处理 ã
。有几种编解码器包含第一个字符但不包含最后一个字符;您正在使用 IBM codepage 437,但它绝不是唯一的。
如果您在标准 Windows 控制台 (cmd.exe
) 中 运行 Python,请注意 Python、Unicode 和该控制台不混合得很好。您可以安装 win-unicode-console
package 使 Python 3 使用 Windows API 更好地输出 Unicode 文本;您需要确保您的字体能够仍然显示您的 Unicode 文本。
我不确定该软件包是否与其他 Windows shell 兼容;您的里程可能会有所不同。
为什么Python在
的输出中添加\xe3
>>> b'Transa\xc3\xa7\xc3\xa3o'.decode('utf-8')
'Transaç\xe3o'
预期值为:
'Transação'
关于我的环境的更多信息:
>>> import sys
>>> print (sys.version)
3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)]
>>> sys.stdout.encoding
'cp437'
这是在 Console 2 + Powershell 下。
您需要使用支持所有 您要打印的字符的控制台或终端。
在交互式控制台中打印时,字符被编码为适合您的控制台的正确编解码器,使用 backslashreplace
error handler to keep the output readable rather than throw an exception. This is a feature of the default sys.displayhook()
function:
If
repr(value)
is not encodable tosys.stdout.encoding
withsys.stdout.errors
error handler (which is probably'strict'
), encode it tosys.stdout.encoding
with'backslashreplace'
error handler.
您的控制台可以处理 ç
但不能处理 ã
。有几种编解码器包含第一个字符但不包含最后一个字符;您正在使用 IBM codepage 437,但它绝不是唯一的。
如果您在标准 Windows 控制台 (cmd.exe
) 中 运行 Python,请注意 Python、Unicode 和该控制台不混合得很好。您可以安装 win-unicode-console
package 使 Python 3 使用 Windows API 更好地输出 Unicode 文本;您需要确保您的字体能够仍然显示您的 Unicode 文本。
我不确定该软件包是否与其他 Windows shell 兼容;您的里程可能会有所不同。