未在终端python中正确打印Unicode字符

未在终端python中正确打印Unicode字符,python,python-3.x,unicode,printing,Python,Python 3.x,Unicode,Printing,我编写了一个非常简单的程序,告诉我一些字符的unicode值 节目如下: #!/usr/bin/env python3 # -*- coding: utf-8 -*- characters = [u'T', u'ב', u'€', u'木', u'♥'] for character in characters: print(character + " has the unicode value :\t"+str(hex(ord(character))) + "\n") 它给出了这

我编写了一个非常简单的程序,告诉我一些字符的unicode值

节目如下:

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

characters = [u'T', u'ב', u'€', u'木', u'♥']

for character in characters:

    print(character + " has the unicode value :\t"+str(hex(ord(character))) + "\n")
它给出了这个输出:

T has the unicode value :   84

ב has the unicode value :   1489

€ has the unicode value :   8364

木 has the unicode value :   26408

♥ has the unicode value :   9829
我注意到,当我在这里复制和粘贴时,输出的格式是正确的,但在我的计算机上,第二行在终端中显示如下

has the unicode value : 1489 ב 

此外,我还尝试将输出放入一个文件中,并使用vim查看该文件,结果也是这样,应该首先打印的字符最后打印。这让我觉得它打印正确,但显示不正确。什么会导致这种情况发生

只需将第一行替换为:

characters = [u'T', u'ב', u'€', u'木', u'♥']

希伯来文字符的右对齐行为可以使用Unicode左右替代(LRO)字符0x202D替代

characters = [u'T', u'ב', u'€', u'木', u'♥']

for character in characters:

    print(chr(0x202D) + character + " has the unicode value :\t"+str(hex(ord(character))) + "\n")
给出(在OS X终端上):

感谢@guribe94识别问题

您可能会发现字符串格式更易于阅读:

    print("%s%s has the unicode value :\t 0x%04x\n" %
        (chr(0x202D), character, ord(character)))

您使用的是什么版本的python?如果您使用的是python2,那么您的代码应该error@PadraicCunningham考虑到在明显的Unicode前面没有
u
,但它仍然给出了
ord
的正确结果,我认为这是Python 3。我不知道哪个子版本。@MarkRansom,我想加倍确定,如果输出不匹配,并且是python3,那么编码很可能是problem@PadraicCunninghamPython 3,抱歉忘了包括that@guribe94:问题可能是
ב
是希伯来文字母。希伯来语是从右向左写的。我真的不知道如何解决这个问题,但我认为在Python3中有一个Unicode字符决定是从左到右还是从右到左写入,这是不必要的,事实上在早期版本中会失败。它在2.7中对我来说很好,如果是Unicode字符串,为什么会失败?实际上,如果不是unicode字符串,它就会崩溃。这并没有什么区别,就像@MarkRansom说的,在python 3中这应该是不必要的,因为python 3将字符串视为unicode字符串default@MarkRansom我在我的“jupyter笔记本”中运行它,一切都很好。@olofom我的意思不是Python 2,而是早于3.3的Python 3。看见抱歉,我不清楚。您可以使用
'\u202d'
代替
chr(0x202D)
    print("%s%s has the unicode value :\t 0x%04x\n" %
        (chr(0x202D), character, ord(character)))