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

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

问题描述

我在WinXP 5.1.2600中工作,编写一个涉及中文拼音的Python应用程序,这涉及我无限的Unicode问题。切换到Python 3.0已经解决了很多。但是控制台输出的print()函数由于某些奇怪的原因不是Unicode感知的。这是一个少年程序。

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:\Python30\lib\io.py", line 1491, in write
        b = encoder.encode(s)
      File "C:\Python30\lib\encodings\cp1252.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]

请注意,ü= \xfc = 252没有问题,因为它是上面的ASCII。但是ā= \\\ā超过8位。

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

任何人都有想法如何将sys.stdout的编码更改为'utf-8'?请记住,如果我理解文档权限,Python 3.0不再使用编解码器模块。

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.

抱歉,我给你的程序没有前言。在给出3行之前,它开始如下:

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

,由coding:行指定的编码是源代码的编码,而不是控制台输出的编码。

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!

推荐答案

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

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.

有关完整说明,请参阅此主题:
http://www.nabble.com/unable-to-print-Unicode-characters-in-Python-3-td21670662.html a>

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天全站免登陆