python 3.0,如何使 print() 输出 unicode? [英] python 3.0, how to make print() output unicode?

查看:16
本文介绍了python 3.0,如何使 print() 输出 unicode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WinXP 5.1.2600 编写一个涉及中文拼音的 Python 应用程序,这让我遇到了无穷无尽的 Unicode 问题.切换到 Python 3.0 解决了其中的许多问题.但是出于某种奇怪的原因,控制台输出的 print() 函数不支持 Unicode.这是一个小程序.

print('sys.stdout 编码为 "' + sys.stdout.encoding + '"')str1 = 'lüelā'打印(str1)

输出是(为了便于阅读,将尖括号改为方括号):

<上一页>sys.stdout 编码为cp1252"回溯(最近一次通话最后):[模块] 中的文件TestPrintEncoding.py",第 22 行打印(str1)文件C:Python30libio.py",第 1491 行,写入b = 编码器.encode(s)文件C:Python30libencodingscp1252.py",第 19 行,在编码中返回 codecs.charmap_encode(input,self.errors,encoding_table)[0]UnicodeEncodeError:charmap"编解码器无法编码字符u0101"在位置 4:字符映射到 [未定义]

请注意 ü = xfc = 252 没有问题,因为它是高位 ASCII.但是 ā = u0101 超过 8 位.

有人知道如何将 sys.stdout 的编码更改为utf-8"吗?请记住,如果我正确理解文档,Python 3.0 不再使用 codecs 模块.

<小时>

抱歉,我给了你没有序言的程序.在给出的 3 行之前,它的开头是这样的:

#!/usr/bin/env python# -*- 编码:utf-8 -*-导入系统

很遗憾,coding:"行指定的编码是源代码的编码,而不是控制台输出的编码.不过谢谢你的想法!

解决方案

Windows 命令提示符 (cmd.exe) 无法显示您正在使用的 Unicode 字符,即使 Python 在内部以正确的方式处理它.您需要使用 IDLE、Cygwin 或其他可以正确显示 Unicode 的程序.

完整的解释请看这个帖子:http://www.nabble.com/unable-to-print-Unicode-characters-in-Python-3-td21670662.html

I'm working in WinXP 5.1.2600, writing a Python application involving Chinese pinyin, which has involved me in endless Unicode problems. Switching to Python 3.0 has solved many of them. But the print() function for console output is not Unicode-aware for some odd reason. Here's a teeny program.

print('sys.stdout encoding is "' + sys.stdout.encoding + '"')
str1 = 'lüelā'
print(str1)

Output is (changing angle brackets to square brackets for readability):

    sys.stdout encoding is "cp1252"
    Traceback (most recent call last):
      File "TestPrintEncoding.py", line 22, in [module]
        print(str1)
      File "C:Python30libio.py", line 1491, in write
        b = encoder.encode(s)
      File "C:Python30libencodingscp1252.py", line 19, in encode
        return codecs.charmap_encode(input,self.errors,encoding_table)[0]
    UnicodeEncodeError: 'charmap' codec can't encode character 'u0101' 
    in position 4: character maps to [undefined]

Note that ü = xfc = 252 gives no problem since it's upper ASCII. But ā = u0101 is beyond 8-bits.

Anyone have an idea how to change the encoding of sys.stdout to 'utf-8'? Bear in mind that Python 3.0 no longer uses the codecs module, if I understand the documentation right.


Apologies, I gave you the program without the preamble. Before the 3 lines given, it starts like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

Unfortunately, the coding specified by the "coding:" line is the coding of the source code, not of the console output. But thank you for your thoughts!

解决方案

The Windows command prompt (cmd.exe) cannot display the Unicode characters you are using, even though Python is handling it in a correct manner internally. You need to use IDLE, Cygwin, or another program that can display Unicode correctly.

See this thread for a full explanation: http://www.nabble.com/unable-to-print-Unicode-characters-in-Python-3-td21670662.html

这篇关于python 3.0,如何使 print() 输出 unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆