在 shell 中打印图标
Printing icons in the shell
下面是一些例子,当运行 inside Python 3.4.3 Shell包含在IDLE3中会输出特殊字符(图标)。当我 运行 在终端内使用相同的代码时,这些字符根本不会出现。
""" 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 ("") # == '\u0001' == '\x01'
#some more fooling around
print ("") #left arrow
print ("") #underlined left arrow
print ("") #right arrow
print ("") #underlined right arrow
print ("") #trinity
print ("") #Q-parking
print ("") #telephone
print ("")
print ("")
print ("") #checkmark
print (" hi") #tab
print (" hi") #newline
print ("") #8th note
print ("") #4th note
print ("") #halve note
print ("") #whole note
print ("") #double 8th note
print ("")
print ("")
print ("") #left arrow (black)
print ("") #right arrow (black)
print ("") #harry potter
print ("") #X-chrom-carrying cell
print ("") #Y-chrom-carrying cell
print ("") #diameter for lefties
print ("") #pentoid
print ("") #gamma?
print ("") #I finally realised this will have to do with triple
# binary per character? 111 = 7, stop = 8
print ("") #
print ("") # female
print ("") # male
print ("") #
print ("") # clock
print ("") # alfa / ichtus
print ("") # arc
print ("") # diameter
print ("hi") # 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 中的输出:
这些 'special' 个字符 c.q。以任何方式使用的图标?是否有一些我可以阅读的文档可以阻止我问这个问题?
我在 Stack 上检查了有关此问题的其他问题,但我发现的只是人们试图传入 'foreign'(例如来自文字符号或其他)字符并使它们由 [=26= 打印].
如果您处于 IDLE 状态并单击 "Options" 和 "Configure IDLE...",您会看到您正在使用的字体。字体将字符编号转换为您所看到的。不同的字体可以产生不同的字符。
示例:
>>> print(u'\u2620')
☠
我通过搜索 "unicode skull" and which can be found here 找到的。
并非所有字体都支持所有字符。
Unicode 字符组织在 blocks of a certain topic. I like the block "Miscellaneous Symbols" 头骨的来源。
编码
还有一个重要的问题是您使用哪种编码。编码决定了字符如何映射到 unicode blocks。角色必须从 print(u'[=14=]01')
到 sys.stdout
到读取它的控制台和 window 管理器。每一步只理解字节——256 个可能的字符。
因此,出现了各种编码,例如 latin-1,它使用 256 个可能的字符并将它们映射到 unicode 块上。我认为 latin-1 使用前两个块。有一些编码,例如使用 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)
我在西里尔代码页和拉丁文代码页中对俄语字母 zhe 进行编码的另一个示例:
>>> 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。
下面是一些例子,当运行 inside Python 3.4.3 Shell包含在IDLE3中会输出特殊字符(图标)。当我 运行 在终端内使用相同的代码时,这些字符根本不会出现。
""" 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 ("") # == '\u0001' == '\x01'
#some more fooling around
print ("") #left arrow
print ("") #underlined left arrow
print ("") #right arrow
print ("") #underlined right arrow
print ("") #trinity
print ("") #Q-parking
print ("") #telephone
print ("")
print ("")
print ("") #checkmark
print (" hi") #tab
print (" hi") #newline
print ("") #8th note
print ("") #4th note
print ("") #halve note
print ("") #whole note
print ("") #double 8th note
print ("")
print ("")
print ("") #left arrow (black)
print ("") #right arrow (black)
print ("") #harry potter
print ("") #X-chrom-carrying cell
print ("") #Y-chrom-carrying cell
print ("") #diameter for lefties
print ("") #pentoid
print ("") #gamma?
print ("") #I finally realised this will have to do with triple
# binary per character? 111 = 7, stop = 8
print ("") #
print ("") # female
print ("") # male
print ("") #
print ("") # clock
print ("") # alfa / ichtus
print ("") # arc
print ("") # diameter
print ("hi") # 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 中的输出:
这些 'special' 个字符 c.q。以任何方式使用的图标?是否有一些我可以阅读的文档可以阻止我问这个问题?
我在 Stack 上检查了有关此问题的其他问题,但我发现的只是人们试图传入 'foreign'(例如来自文字符号或其他)字符并使它们由 [=26= 打印].
如果您处于 IDLE 状态并单击 "Options" 和 "Configure IDLE...",您会看到您正在使用的字体。字体将字符编号转换为您所看到的。不同的字体可以产生不同的字符。
示例:
>>> print(u'\u2620')
☠
我通过搜索 "unicode skull" and which can be found here 找到的。
并非所有字体都支持所有字符。
Unicode 字符组织在 blocks of a certain topic. I like the block "Miscellaneous Symbols" 头骨的来源。
编码
还有一个重要的问题是您使用哪种编码。编码决定了字符如何映射到 unicode blocks。角色必须从 print(u'[=14=]01')
到 sys.stdout
到读取它的控制台和 window 管理器。每一步只理解字节——256 个可能的字符。
因此,出现了各种编码,例如 latin-1,它使用 256 个可能的字符并将它们映射到 unicode 块上。我认为 latin-1 使用前两个块。有一些编码,例如使用 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)
我在西里尔代码页和拉丁文代码页中对俄语字母 zhe 进行编码的另一个示例:
>>> 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。