Search code examples
pythonunicodeasciicyrillicord

How to get ASCII cyrillic character code in Python?


ord() returns unicode code, and I need ascii.

>>> s = "Йог" #cyrillic
>>> for char in s:
...     print(ord(char))
... 
1049 #unicode
1086 #unicode
1075 #unicode

and I need ASCII. How to get it? (below)

enter image description here


Solution

  • You can't; there are no Cyrillic characters in ASCII. The chart you've shown is for one of the many "extended ASCII" character sets; specifically, it appears to be Windows-1251 (a.k.a. CP1251). In order to get a character's codepoint in this encoding, you thus need to first encode the string as CP1251 and then take the value of the resulting byte:

    # Assuming Python 3
    s = "Йог".encode('cp1251')
    for b in s:
        print(b)