有没有包含所有ascii字符列表的Python库?

问题:有没有包含所有ascii字符列表的Python库?

如下所示:

import ascii

print ascii.charlist()

会返回类似[A,B,C,D …]的内容

Something like below:

import ascii

print ascii.charlist()

Which would return something like [A, B, C, D…]


回答 0

string常数可能是你想要的东西。(docs

>>>导入字符串
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

如果要所有可打印字符:

>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!“#$%&\'()* +,-。/:;?@ [\\] ^ _`{|}〜\ t \ n \ r \ x0b \ x0c'

The string constants may be what you want. (docs)

>>> import string
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

If you want all printable characters:

>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

回答 1

这里是:

[chr(i) for i in xrange(127)]

Here it is:

[chr(i) for i in xrange(127)]

回答 2

ASCII定义128个字符,其字节值范围从0到127(包括0和127)。因此,要获取所有ASCII字符的字符串,您可以

''.join([chr(i) for i in range(128)])

其中只有一些是可打印的,但是-可打印的ASCII字符可以通过Python通过以下方式访问

import string
string.printable

ASCII defines 128 characters whose byte values range from 0 to 127 inclusive. So to get a string of all the ASCII characters, you could just do

''.join([chr(i) for i in range(128)])

Only some of those are printable, however- the printable ASCII characters can be accessed in Python via

import string
string.printable

回答 3

由于ASCII可打印字符是一个很小的列表(值在32到127之间的字节),因此在需要时很容易生成:

>>> for c in (chr(i) for i in range(32,127)):
...     print c
... 

!
"
#
$
%
... # a few lines removed :)
y
z
{
|
}
~

Since ASCII printable characters are a pretty small list (bytes with values between 32 and 127), it’s easy enough to generate when you need:

>>> for c in (chr(i) for i in range(32,127)):
...     print c
... 

!
"
#
$
%
... # a few lines removed :)
y
z
{
|
}
~

回答 4

for i in range(0,128):
    print chr(i)

试试这个!

for i in range(0,128):
    print chr(i)

Try this!


回答 5

您可以在没有模块的情况下执行此操作:

    characters = list(map(chr, range(97,123)))

输入characters并应打印["a","b","c", ... ,"x","y","z"]。对于大写使用:

    characters=list(map(chr,range(65,91)))

可以使用任何范围(包括使用范围步骤),因为它使用Unicode。因此,增加range()将更多字符添加到列表中。
map()调用的chr()每次迭代range()

You can do this without a module:

    characters = list(map(chr, range(97,123)))

Type characters and it should print ["a","b","c", ... ,"x","y","z"]. For uppercase use:

    characters=list(map(chr,range(65,91)))

Any range (including the use of range steps) can be used for this, because it makes use of Unicode. Therefore, increase the range() to add more characters to the list.
map() calls chr() every iteration of the range().


回答 6

不,没有,但是您可以轻松地制作一个:

    #Your ascii.py program:
    def charlist(begin, end):
        charlist = []
        for i in range(begin, end):
            charlist.append(chr(i))
        return ''.join(charlist)

    #Python shell:
    #import ascii
    #print(ascii.charlist(50, 100))
    #Comes out as:

    #23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abc

No, there isn’t, but you can easily make one:

    #Your ascii.py program:
    def charlist(begin, end):
        charlist = []
        for i in range(begin, end):
            charlist.append(chr(i))
        return ''.join(charlist)

    #Python shell:
    #import ascii
    #print(ascii.charlist(50, 100))
    #Comes out as:

    #23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abc