Python 如何将数字转换为字母表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18544419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:59:36  来源:igfitidea点击:

How to convert numbers to alphabet?

python

提问by user2734815

I read this threadabout converting the alphabet to numbers but I don't understand how to convert the numbers back into letters. I would appreciate if someone could expand on that, especially and more specifically, the chr()function described in the thread. I've already tried searching for the chrfunction but there aren't many tutorials for it.

我读了这个关于将字母转换为数字的线程,但我不明白如何将数字转换回字母。如果有人可以对此进行扩展,特别是更具体地说,chr()线程中描述的功能,我将不胜感激。我已经尝试搜索该chr功能,但没有太多教程。

回答by thefourtheye

If you have a number, for example 65, and if you want to get the corresponding ASCII character, you can use the chrfunction, like this

如果你有一个数字,比如 65,并且你想得到相应的 ASCII 字符,你可以使用这个chr函数,像这样

>>> chr(65)
'A'

similarly if you have 97,

同样,如果你有 97,

>>> chr(97)
'a'


EDIT:The above solution works for 8 bit characters or ASCII characters. If you are dealing with unicode characters, you have to specify unicode value of the starting character of the alphabet to ordand the result has to be converted using unichrinstead of chr.

编辑:上述解决方案适用于 8 位字符或 ASCII 字符。如果您正在处理 unicode 字符,则必须将字母表的起始字符的 unicode 值指定为 toord并且必须使用unichr而不是来转换结果chr

>>> print unichr(ord(u'\u0B85'))
?

>>> print unichr(1 + ord(u'\u0B85'))
?

NOTE:The unicode characters used here are of the language called "Tamil", my first language. This is the unicode table for the same http://www.unicode.org/charts/PDF/U0B80.pdf

注意:此处使用的 unicode 字符属于称为“泰米尔语”的语言,这是我的第一语言。这是同一http://www.unicode.org/charts/PDF/U0B80.pdf的 unicode 表