2015-10-04 143 views
0

下面是一些在IDLE3中包含的Python 3.4.3 Shell中運行的例子,它將輸出特殊字符(圖標)。當我在終端中運行這個相同的代碼時,這些字符根本不會出現。在shell中打印圖標

""" Some print functions with backslashes. 
    In IDLE3 they will output 'special characters' or icons. 
    In a terminal, they will not output anything. """ 

#Somtimes a visual effect. 
print ("a, \a") #telephone 
print ("\a") 
print ("b, \b") #checkmark 
print ("c, \c") # just a '\c' output. 
# other letters like '\c' kept out the rest of this list. 
print ("f, \f") #quarter note (musical) 
print ("n, \n") #newline 
print ("r, \r") #halve note (musical) 
print ("t, \tTabbed in") 
#print ("u, \u") #syntaxerror 
print ("\u0000") #empty 
print ("\u0001") #left arrow 
print ("\u0002") #left arrow underline 
print ("\u0003") #right arrow (play) 
print ("v, \v") #eighth note (musical) 
print ("\x01") # == '\u0001' __________(x == 00 ?) 
print ("\1") # == '\u0001' == '\x01' 

#some more fooling around 
print ("\1") #left arrow 
print ("\2") #underlined left arrow 
print ("\3") #right arrow 
print ("\4") #underlined right arrow 
print ("\5") #trinity 
print ("\6") #Q-parking 
print ("\7") #telephone 
print ("\8") 
print ("\9") 
print ("\10") #checkmark 
print ("\11 hi") #tab 
print ("\12 hi") #newline 
print ("\13") #8th note 
print ("\14") #4th note 
print ("\15") #halve note 
print ("\16") #whole note 
print ("\17") #double 8th note 
print ("\18") 
print ("\19") 
print ("\20") #left arrow (black) 
print ("\21") #right arrow (black) 
print ("\22") #harry potter 
print ("\23") #X-chrom-carrying cell 
print ("\24") #Y-chrom-carrying cell 
print ("\25") #diameter for lefties 
print ("\26") #pentoid 
print ("\27") #gamma? 
print ("\28") #I finally realised this will have to do with triple 
       # binary per character? 111 = 7, stop = 8 

print ("\30") # 
print ("\31") # female 
print ("\32") # male 
print ("\33") # 
print ("\34") # clock 
print ("\35") # alfa/ichtus 
print ("\36") # arc 
print ("\37") # diameter 

print ("\40hi") # spaces? I don't know. 


# This does not work by the way: 

##import string 

###No visual effect. 
##alfa = string.ascii_lowercase 
##for x in alfa: 
## print ("\%s" % x) 

我的一些Python Shell 3.4.3。在IDLE3輸出:

IDLE output

難道這些 '特殊' 字符C.Q。以任何方式使用的圖標?是否有一些我可以閱讀的文檔可以阻止我提出這個問題?

我在Stack上檢查了關於這個問題的其他問題,但是我發現所有人都試圖通過「外部」(如來自文字符號或任何其他字符)的字符,並使它們被Python打印。

+0

鑑於此問題涉及IDLE和您的終端之間的不同行爲,*哪個終端*? – jonrsharpe

+0

Ubuntu 14中的終端仿真器。 內核:3.19.0-28-generic 這有幫助嗎? – Sonder

+0

不是所有的都是可打印的字符,我想怠速不能正確打印輸出 –

回答

1

如果您處於IDLE狀態,然後單擊「選項」和「配置IDLE ...」,您將看到正在使用的字體。字體將字符數字轉換爲您所看到的。不同的字體可以產生不同的字符。

例子:

>>> print(u'\u2620') 
☠ 

哪個我看着通過搜索"unicode skull"和可發現here

並非所有字體都支持所有字符。

Unicode字符組織在某個主題的blocks中。我喜歡"Miscellaneous Symbols"的頭骨來自哪裏。

編碼

另外一個重要的問題是你使用哪種編碼。編碼決定了字符如何映射到unicode blocks。角色必須從print(u'\0001')sys.stdout到控制檯讀取它和窗口管理器。每一步只能理解字節 - 256個可能的字符。

因此,有各種編碼,如拉丁-1,它使用256個可能的字符並將它們映射到unicode塊。拉丁一號使用前兩個街區,我想。有一些編碼,例如使用8位= 1字節或更多的UTF-8或使用2字節或更多的UTF-16或使用4字節或更多的UTF-32,這些編碼允許更多字符從打印轉移到不同的步驟。

如果我要編碼的骷髏頭的Latin-1,我會得到這個錯誤:

>>> u'\u2620'.encode('latin-1') 

Traceback (most recent call last): 
    File "<pyshell#32>", line 1, in <module> 
    u'\u2620'.encode('latin-1') 
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2620' in position 0: ordinal not in range(256) 

,我編碼西里爾代碼頁俄羅斯信哲和拉丁一個又如:

>>> print u'\u0436', repr(u'\u0436'.encode('cp1251')) # cyrillic works 
ж '\xe6' 
>>> print u'\u0436', repr(u'\u0436'.encode('cp1252')) # latin-1 fails 
ж 

Traceback (most recent call last): 
    File "<pyshell#41>", line 1, in <module> 
    print u'\u0436', repr(u'\u0436'.encode('cp1252')) # latin-1 
    File "C:\Python27\lib\encodings\cp1252.py", line 12, in encode 
    return codecs.charmap_encode(input,errors,encoding_table) 
UnicodeEncodeError: 'charmap' codec can't encode character u'\u0436' in position 0: character maps to <undefined> 

爲了避開這種編碼叢林,使用UTF-8可編碼的一切。

>>> print u'\u0436\u2620', repr(u'\u0436\u2620'.encode('utf-8')) 
ж☠ '\xd0\xb6\xe2\x98\xa0' 

使用不同的編碼進行編碼和解碼會改變字符。如果您想使用有趣的字符,請使用unicode和UTF-8。