如何使用 Python 获取所有 ASCII 字符的列表?

发布于 2024-11-05 10:28:11 字数 143 浏览 0 评论 0原文

我正在寻找类似以下内容的内容:

import ascii

print(ascii.charlist())

它将返回类似 ["A", "B", "C", "D" ... ] 的内容。

I'm looking for something like the following:

import ascii

print(ascii.charlist())

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

壹場煙雨 2024-11-12 10:28:11

string 模块中的常量可能是你想要什么。

所有 ASCII 大写字母:

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

所有 可打印 ASCII 字符:

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

对于每个单个字符ASCII 标准中定义的字符,请使用 chr

>>> ''.join(chr(i) for i in range(128))
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f'

The constants in the string module may be what you want.

All ASCII capital letters:

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

All printable ASCII characters:

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

For every single character defined in the ASCII standard, use chr:

>>> ''.join(chr(i) for i in range(128))
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f'
世俗缘 2024-11-12 10:28:11

这里是:

[chr(i) for i in range(128)]

Here it is:

[chr(i) for i in range(128)]
溺孤伤于心 2024-11-12 10:28:11

ASCII 定义了 128 个字符,其字节值范围为 0 到 127(含)。因此,要获取所有 ASCII 字符的字符串,您只需执行“

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

仅其中 100 个字符被视为可打印”。可打印的 ASCII 字符可以通过以下方式访问

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 100 of those are considered printable. The printable ASCII characters can be accessed via

import string
string.printable
滥情哥ㄟ 2024-11-12 10:28:11

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

>>> 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 126 inclusive), 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
{
|
}
~
那请放手 2024-11-12 10:28:11

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

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

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

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

任何范围(包括范围步骤的使用)都可以用于此目的,因为它使用 Unicode。因此,增加 range() 以向列表中添加更多字符。

map()range() 的每次迭代中调用 chr()

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().

池予 2024-11-12 10:28:11
for i in range(0, 128):
    print(chr(i))
for i in range(0, 128):
    print(chr(i))
不气馁 2024-11-12 10:28:11

不,没有,但你可以轻松制作一个:

    #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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文