我写了一个非常简单的程序,告诉我某些字符的unicode值。Unicode字符在终端python中没有正确打印
下面是程序:
#!/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查看文件,它看起来像这样,应该首先打印的字符最后打印。这导致我认为它正在正确打印,但不能正确显示。什么可能导致这种情况发生?
您正在使用什么版本的Python?如果你正在使用python2,你的代码应该错误 –
@PadraicCunningham,因为在明显的Unicode前没有'u',但它仍然从'ord'给出正确的结果,我会说它是Python 3.哪个子版本我不知道。 –
@MarkRansom,我想双倍肯定,如果输出不匹配,它是python3,那么编码很可能是问题 –