我寫了一個非常簡單的程序,告訴我某些字符的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,那麼編碼很可能是問題 –