cri*_*ter 3 python unicode encoding decode python-3.x
这个字符串:
\n\nline = \'\\tlong_plugin_output=\\x88\\\\\\\\\\x97\\xe5\\xff\\x7f\\\\n\'\nRun Code Online (Sandbox Code Playgroud)\n\n.. 打印时,在我的 Macbook Pro 上产生以下输出:
\n\n>>> line = \'\\tlong_plugin_output=\\x88\\\\\\\\\\x97\\xe5\\xff\\x7f\\\\n\'\n\n>>> line\n\'\\tlong_plugin_output=\\x88\\\\\\\\\\x97\xc3\xa5\xc3\xbf\\x7f\\\\n\'\n\n>>> print(line)\n long_plugin_output=\\\\\xc3\xa5\xc3\xbf\\n\nRun Code Online (Sandbox Code Playgroud)\n\n..但是它在我的 ubuntu 服务器上产生了这个错误:
\n\n>>> line = \'\\tlong_plugin_output=\\x88\\\\\\\\\\x97\\xe5\\xff\\x7f\\\\n\'\n\n>>> line\n\'\\tlong_plugin_output=\\x88\\\\\\\\\\x97\\xe5\\xff\\x7f\\\\n\'\n\n>>> print(line)\nTraceback (most recent call last):\n File "<stdin>", line 1, in <module>\nUnicodeEncodeError: \'ascii\' codec can\'t encode character \'\\x88\' in position 20: ordinal not in range(128)\nRun Code Online (Sandbox Code Playgroud)\n\n我在 macbook 上运行的 python 版本:
\n\n\n\n\nPython 3.3.2(v3.3.2:d047928ae3f6,2013 年 5 月 13 日,13:52:24)[GCC 4.2.1\n(Apple Inc. build 5666)(点 3)] 达尔文
\n\n$ uname -a Darwin MacBook-Pro.local 11.4.2 Darwin 内核版本\n 11.4.2:2012 年 8 月 23 日星期四 16:25:48 PDT;根目录:xnu-1699.32.7~1/RELEASE_X86_64 x86_64
\n
我在 Ubuntu 服务器上运行的 python 版本:
\n\n\n\n\nlinux2 上的 Python 3.2.3(默认,2013 年 9 月 25 日,18:25:56)[GCC 4.6.3]
\n\n$ uname -a Linux net.local.net 3.2.2 #3 SMP 1 月 26 日星期四 20:18:37 UTC\n 2012 i686 i686 i386 GNU/Linux
\n
是什么导致这些平台上出现不同的行为?
\nPython 询问终端正在使用什么编码,并在打印时将 unicode 字符串编码为字节。您的 Ubuntu 服务器未配置为 UTF-8 显示,您的 Mac 终端是.
有关切换终端区域设置的帮助,请参阅https://askubuntu.com/questions/87227/switch-encoding-of-terminal-with-a-command 。任何可以处理您尝试打印的特定代码点的语言环境都可以,但 UTF8 可以处理所有 Unicode。
你可以通过打印来查看Python检测到的内容sys.stdout.encoding:
>>> import sys
>>> sys.stdout.encoding
'UTF-8'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2473 次 |
| 最近记录: |