我意识到我可以使用内置ord
的unichr
python 函数来获取字符的 unicode,反之亦然。但是,是否有 ASCI 字符集中所有可能字符的列表,我可以通过调用 python 中的函数或某些 API 来访问?
换句话说,我可以在这里访问此列表吗?通过一些函数调用?
谢谢
底部的 128 个 Unicode 代码点是ASCII,因此,只要您将自己限制在 0 到 127 之间的序数值unichr
(或仅chr
在 Python 3 中)就可以正常工作。
例如,下面的 Python 2.6.6 程序(当然是老式的,但我更喜欢这种形式的教学):
print " | 0 1 2 3 4 5 6 7 8 9 A B C D E F"
print "-+--------------------------------"
for row in range(2,8):
s = "%d|"%(row)
for col in range(0,16):
if row != 7 or col != 15: # 7F is non-printable
s = "%s %s"%(s,unichr(row * 16 + col))
print s
为您输出 ASCII 表(好吧,无论如何,可打印位):
| 0 1 2 3 4 5 6 7 8 9 A B C D E F
-+--------------------------------
2| ! " # $ % & ' ( ) * + , - . /
3| 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4| @ A B C D E F G H I J K L M N O
5| P Q R S T U V W X Y Z [ \ ] ^ _
6| ` a b c d e f g h i j k l m n o
7| p q r s t u v w x y z { | } ~
>>> from unicodedata import name, category
>>> for i in range(128):
c = unichr(i)
print hex(i), i, category(c), name(c, '--')
0x0 0 Cc --
0x1 1 Cc --
0x2 2 Cc --
0x3 3 Cc --
0x4 4 Cc --
0x5 5 Cc --
0x6 6 Cc --
0x7 7 Cc --
0x8 8 Cc --
0x9 9 Cc --
0xa 10 Cc --
0xb 11 Cc --
0xc 12 Cc --
0xd 13 Cc --
0xe 14 Cc --
0xf 15 Cc --
0x10 16 Cc --
0x11 17 Cc --
0x12 18 Cc --
0x13 19 Cc --
0x14 20 Cc --
0x15 21 Cc --
0x16 22 Cc --
0x17 23 Cc --
0x18 24 Cc --
0x19 25 Cc --
0x1a 26 Cc --
0x1b 27 Cc --
0x1c 28 Cc --
0x1d 29 Cc --
0x1e 30 Cc --
0x1f 31 Cc --
0x20 32 Zs SPACE
0x21 33 Po EXCLAMATION MARK
0x22 34 Po QUOTATION MARK
0x23 35 Po NUMBER SIGN
0x24 36 Sc DOLLAR SIGN
0x25 37 Po PERCENT SIGN
0x26 38 Po AMPERSAND
0x27 39 Po APOSTROPHE
0x28 40 Ps LEFT PARENTHESIS
0x29 41 Pe RIGHT PARENTHESIS
0x2a 42 Po ASTERISK
0x2b 43 Sm PLUS SIGN
0x2c 44 Po COMMA
0x2d 45 Pd HYPHEN-MINUS
0x2e 46 Po FULL STOP
0x2f 47 Po SOLIDUS
0x30 48 Nd DIGIT ZERO
0x31 49 Nd DIGIT ONE
0x32 50 Nd DIGIT TWO
0x33 51 Nd DIGIT THREE
0x34 52 Nd DIGIT FOUR
0x35 53 Nd DIGIT FIVE
0x36 54 Nd DIGIT SIX
0x37 55 Nd DIGIT SEVEN
0x38 56 Nd DIGIT EIGHT
0x39 57 Nd DIGIT NINE
0x3a 58 Po COLON
0x3b 59 Po SEMICOLON
0x3c 60 Sm LESS-THAN SIGN
0x3d 61 Sm EQUALS SIGN
0x3e 62 Sm GREATER-THAN SIGN
0x3f 63 Po QUESTION MARK
0x40 64 Po COMMERCIAL AT
0x41 65 Lu LATIN CAPITAL LETTER A
0x42 66 Lu LATIN CAPITAL LETTER B
0x43 67 Lu LATIN CAPITAL LETTER C
0x44 68 Lu LATIN CAPITAL LETTER D
0x45 69 Lu LATIN CAPITAL LETTER E
0x46 70 Lu LATIN CAPITAL LETTER F
0x47 71 Lu LATIN CAPITAL LETTER G
0x48 72 Lu LATIN CAPITAL LETTER H
0x49 73 Lu LATIN CAPITAL LETTER I
0x4a 74 Lu LATIN CAPITAL LETTER J
0x4b 75 Lu LATIN CAPITAL LETTER K
0x4c 76 Lu LATIN CAPITAL LETTER L
0x4d 77 Lu LATIN CAPITAL LETTER M
0x4e 78 Lu LATIN CAPITAL LETTER N
0x4f 79 Lu LATIN CAPITAL LETTER O
0x50 80 Lu LATIN CAPITAL LETTER P
0x51 81 Lu LATIN CAPITAL LETTER Q
0x52 82 Lu LATIN CAPITAL LETTER R
0x53 83 Lu LATIN CAPITAL LETTER S
0x54 84 Lu LATIN CAPITAL LETTER T
0x55 85 Lu LATIN CAPITAL LETTER U
0x56 86 Lu LATIN CAPITAL LETTER V
0x57 87 Lu LATIN CAPITAL LETTER W
0x58 88 Lu LATIN CAPITAL LETTER X
0x59 89 Lu LATIN CAPITAL LETTER Y
0x5a 90 Lu LATIN CAPITAL LETTER Z
0x5b 91 Ps LEFT SQUARE BRACKET
0x5c 92 Po REVERSE SOLIDUS
0x5d 93 Pe RIGHT SQUARE BRACKET
0x5e 94 Sk CIRCUMFLEX ACCENT
0x5f 95 Pc LOW LINE
0x60 96 Sk GRAVE ACCENT
0x61 97 Ll LATIN SMALL LETTER A
0x62 98 Ll LATIN SMALL LETTER B
0x63 99 Ll LATIN SMALL LETTER C
0x64 100 Ll LATIN SMALL LETTER D
0x65 101 Ll LATIN SMALL LETTER E
0x66 102 Ll LATIN SMALL LETTER F
0x67 103 Ll LATIN SMALL LETTER G
0x68 104 Ll LATIN SMALL LETTER H
0x69 105 Ll LATIN SMALL LETTER I
0x6a 106 Ll LATIN SMALL LETTER J
0x6b 107 Ll LATIN SMALL LETTER K
0x6c 108 Ll LATIN SMALL LETTER L
0x6d 109 Ll LATIN SMALL LETTER M
0x6e 110 Ll LATIN SMALL LETTER N
0x6f 111 Ll LATIN SMALL LETTER O
0x70 112 Ll LATIN SMALL LETTER P
0x71 113 Ll LATIN SMALL LETTER Q
0x72 114 Ll LATIN SMALL LETTER R
0x73 115 Ll LATIN SMALL LETTER S
0x74 116 Ll LATIN SMALL LETTER T
0x75 117 Ll LATIN SMALL LETTER U
0x76 118 Ll LATIN SMALL LETTER V
0x77 119 Ll LATIN SMALL LETTER W
0x78 120 Ll LATIN SMALL LETTER X
0x79 121 Ll LATIN SMALL LETTER Y
0x7a 122 Ll LATIN SMALL LETTER Z
0x7b 123 Ps LEFT CURLY BRACKET
0x7c 124 Sm VERTICAL LINE
0x7d 125 Pe RIGHT CURLY BRACKET
0x7e 126 Sm TILDE
0x7f 127 Cc --
是否有 ASCI 字符集中所有可能字符的列表,我可以通过调用 python 中的函数或某些 API 来访问?
不,没有内置支持,但你总是可以用几个内置支持滚动一个
map(unichr, range(128))
如果使用 Py3.X,由于没有 unicode 和 non-unicode 的区别,你需要使用chr
. 还map
返回一个map
对象,需要将其转换为list
list(map(chr, range(128)))
相反,如果您需要所有 ascii 字符的字符串,则可以同时加入结果列表
''.join(map(unichr, range(128)))
''.join(map(chr, range(128)))