问题:如何获取字符的ASCII值

如何在Python中获取字符的ASCII值?int

How do I get the ASCII value of a character as an int in Python?


回答 0

这里

函数ord()将获取char的int值。如果您想在玩完数字后再转换回去,可以使用chr()函数来解决。

>>> ord('a')
97
>>> chr(97)
'a'
>>> chr(ord('a') + 3)
'd'
>>>

在Python 2中,还有一个unichr函数,返回其序数为参数的Unicode字符unichr

>>> unichr(97)
u'a'
>>> unichr(1234)
u'\u04d2'

在Python 3中,您可以使用chr代替unichr


ord()-Python 3.6.5rc1文档

ord()-Python 2.7.14文档

From here:

function ord() would get the int value of the char. And in case you want to convert back after playing with the number, function chr() does the trick.

>>> ord('a')
97
>>> chr(97)
'a'
>>> chr(ord('a') + 3)
'd'
>>>

In Python 2, there is also the unichr function, returning the Unicode character whose ordinal is the unichr argument:

>>> unichr(97)
u'a'
>>> unichr(1234)
u'\u04d2'

In Python 3 you can use chr instead of unichr.


ord() – Python 3.6.5rc1 documentation

ord() – Python 2.7.14 documentation


回答 1

请注意,这ord()本身并不能提供ASCII值;它以任何编码形式为您提供字符的数值。因此,ord('ä')如果您使用的是Latin-1,则结果可以为228,如果您使用的TypeError是UTF-8 ,则其结果可以为a 。如果您传递一个unicode,它甚至可以返回Unicode代码点:

>>> ord(u'あ')
12354

Note that ord() doesn’t give you the ASCII value per se; it gives you the numeric value of the character in whatever encoding it’s in. Therefore the result of ord('ä') can be 228 if you’re using Latin-1, or it can raise a TypeError if you’re using UTF-8. It can even return the Unicode codepoint instead if you pass it a unicode:

>>> ord(u'あ')
12354

回答 2

您正在寻找:

ord()

You are looking for:

ord()

回答 3

公认的答案是正确的,但是如果您需要一次将一大堆ASCII字符转换为它们的ASCII代码,则可以采用一种更聪明/更有效的方法。而不是做:

for ch in mystr:
    code = ord(ch)

或稍快:

for code in map(ord, mystr):

您将转换为直接对代码进行迭代的Python本机类​​型。在Python 3上,这很简单:

for code in mystr.encode('ascii'):

在Python 2.6 / 2.7上,它涉及的只是一点点,因为它没有Py3样式的bytes对象(bytes是的别名str,它是按字符迭代的),但是它们确实有bytearray

# If mystr is definitely str, not unicode
for code in bytearray(mystr):

# If mystr could be either str or unicode
for code in bytearray(mystr, 'ascii'):

编码为按序本机迭代的类型意味着转换要快得多。在Py2.7和Py3.5的本地测试中,使用进行迭代str以获取其ASCII码map(ord, mystr)开始关闭需要大约两倍的时间为len10 str比使用bytearray(mystr)上的Py2或mystr.encode('ascii')在PY3,并作为str变长,乘数支付map(ord, mystr)上升至〜6.5x-7x。

唯一的缺点是转换是一次完成的,因此您的第一个结果可能会花费更长的时间,而真正巨大的str临时结果会成比例地增加bytes /bytearray,但是除非迫使您进入页面崩溃状态,否则这无关紧要。

The accepted answer is correct, but there is a more clever/efficient way to do this if you need to convert a whole bunch of ASCII characters to their ASCII codes at once. Instead of doing:

for ch in mystr:
    code = ord(ch)

or the slightly faster:

for code in map(ord, mystr):

you convert to Python native types that iterate the codes directly. On Python 3, it’s trivial:

for code in mystr.encode('ascii'):

and on Python 2.6/2.7, it’s only slightly more involved because it doesn’t have a Py3 style bytes object (bytes is an alias for str, which iterates by character), but they do have bytearray:

# If mystr is definitely str, not unicode
for code in bytearray(mystr):

# If mystr could be either str or unicode
for code in bytearray(mystr, 'ascii'):

Encoding as a type that natively iterates by ordinal means the conversion goes much faster; in local tests on both Py2.7 and Py3.5, iterating a str to get its ASCII codes using map(ord, mystr) starts off taking about twice as long for a len 10 str than using bytearray(mystr) on Py2 or mystr.encode('ascii') on Py3, and as the str gets longer, the multiplier paid for map(ord, mystr) rises to ~6.5x-7x.

The only downside is that the conversion is all at once, so your first result might take a little longer, and a truly enormous str would have a proportionately large temporary bytes/bytearray, but unless this forces you into page thrashing, this isn’t likely to matter.


回答 4

要获取字符的ASCII码,您可以使用 ord()函数。

这是示例代码:

value = input("Your value here: ")
list=[ord(ch) for ch in value]
print(list)

输出:

Your value here: qwerty
[113, 119, 101, 114, 116, 121]

To get the ASCII code of a character, you can use the ord() function.

Here is an example code:

value = input("Your value here: ")
list=[ord(ch) for ch in value]
print(list)

Output:

Your value here: qwerty
[113, 119, 101, 114, 116, 121]

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。