为什么我不能在Mac OS X Terminal.app上的Python解释器中显示Unicode字符? [英] Why can't I display a unicode character in the Python Interpreter on Mac OS X Terminal.app?

查看:132
本文介绍了为什么我不能在Mac OS X Terminal.app上的Python解释器中显示Unicode字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试粘贴unicode字符(例如中间点):

If I try to paste a unicode character such as the middle dot:

·

在我的python解释器中,它什么也没做.我在Mac OS X上使用Terminal.app,当我只是以bash身份参加时,我不会遇到任何麻烦:

in my python interpreter it does nothing. I'm using Terminal.app on Mac OS X and when I'm simply in in bash I have no trouble:

:~$ ·

但是在解释器中:

:~$ python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

^^我什么也没得到,只是忽略了我刚刚粘贴的角色.如果我使用中间点'\ xc2 \ xb7'的转义\ xNN \ xNN表示,并尝试转换为unicode,则尝试显示该点会导致解释器抛出错误:

^^ I get nothing, it just ignores that I just pasted the character. If I use the escape \xNN\xNN representation of the middle dot '\xc2\xb7', and try to convert to unicode, trying to show the dot causes the interpreter to throw an error:

>>> unicode('\xc2\xb7')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)

我在sitecustomize.py中将"utf-8"设置为默认编码,所以:

I have setup 'utf-8' as my default encoding in sitecustomize.py so:

>>> sys.getdefaultencoding()
'utf-8'

有什么作用?这不是航站楼.不是Python,我在做什么错?!

What gives? It's not the Terminal. It's not Python, what am I doing wrong?!

该问题与此问题无关.个人可以将unicode粘贴到他的终端中.

This question is not related to this question, as that indivdiual is able to paste unicode into his Terminal.

推荐答案

unicode('\xc2\xb7')的意思是使用默认编解码器ascii解码有问题的字节字符串-当然会失败(尝试设置其他默认值)编码从来没有很好地工作过,特别是不适用于粘贴文字"-仍然需要 different 设置).您可以改用u'\ xc2 \ xb7',然后查看:

unicode('\xc2\xb7') means to decode the byte string in question with the default codec, which is ascii -- and that of course fails (trying to set a different default encoding has never worked well, and in particular doesn't apply to "pasted literals" -- that would require a different setting anyway). You could use instead u'\xc2\xb7', and see:

>>> print(u'\xc2\xb7')
·

因为这些当然是两个 Unicode字符.时间:

since those are two unicode characters of course. While:

>>> print(u'\uc2b7')
슷

给您一个unicode字符(具有某种东方的说服力-抱歉,我对这些事情一无所知).顺便说一句,这些都不是您想要的中间点".也许你是说

gives you a single unicode character (of some oriental persuasion -- sorry, I'm ignorant about these things). BTW, neither of these is the "middle dot" you were looking for. Maybe you mean

>>> print('\xc2\xb7'.decode('utf8'))
·

其中中间点.顺便说一句,对我来说(在Mac Terminal.app上是python.org的python 2.6.4):

which is the middle dot. BTW, for me (python 2.6.4 from python.org on a Mac Terminal.app):

>>> print('슷')
슷

哪种让我感到惊讶(我预料到会出现错误...!-).

which kind of surprised me (I expected an error...!-).

这篇关于为什么我不能在Mac OS X Terminal.app上的Python解释器中显示Unicode字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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