获取US ASCII字符集 - Python

时间:2013-02-20 07:27:02

标签: python unicode ascii

我意识到我可以使用ordunichr内置的python函数来获取字符的unicode,反之亦然。但是,是否有一个ASCI字符集中所有可能字符的列表,我可以通过在python中调用函数或某些API来访问它?

换句话说,我可以通过某个函数调用访问此列表here?吗?

由于

3 个答案:

答案 0 :(得分:3)

底部的128个Unicode代码点 ASCII所以,只要您将自己限制在0到127之间的序数值,unichr(或仅chr in 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 { | } ~

答案 1 :(得分:2)

>>> 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 --

答案 2 :(得分:1)

  

是否有ASCI字符集中所有可能字符的列表   通过在python中调用函数或某些API进行访问?

没有没有内置支持,但你总是可以使用几个内置的支持

map(unichr, range(128))

如果使用Py3.X,因为unicode和非unicode之间没有区别,您需要使用chr。此外,map会返回map个对象,需要将其转换为list

list(map(chr, range(128)))

相反,如果您需要所有ascii字符的字符串,则可以加入结果列表

''.join(map(unichr, range(128)))

''.join(map(chr, range(128)))