如何获取 Python 中的 ASCII 西里尔字符代码?
How to get ASCII cyrillic character code in Python?
ord()
returns unicode码,我需要ascii。
>>> s = "Йог" #cyrillic
>>> for char in s:
... print(ord(char))
...
1049 #unicode
1086 #unicode
1075 #unicode
我需要 ASCII。如何获得? (下)
你不能; 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) 中没有西里尔字符。为了获得此编码中字符的代码点,您需要首先将字符串编码为 CP1251,然后获取结果字节的值:
# Assuming Python 3
s = "Йог".encode('cp1251')
for b in s:
print(b)
glagolitsa = "А,Б,В,Г,Д,Е,Ё,Ж,З,И,Й,К,Л,М,Н,О,П,Р,С,Т,У,Ф,Х,Ц,Ч,Ш,Щ,Ъ,Ы,Ь,Э,Ю,Я"
Glagolitsa = glagolitsa.split(',')
Glagolitsa
for i in range(len(Glagolitsa)):
char = Glagolitsa[i]
print(ord(char))
glagolitsa = glagolitsa.lower().split(',')
for i in range(len(glagolitsa)):
char = glagolitsa[i]
print(ord(char))
for i in range(1040, 1104):
print(chr(i))
ord()
returns unicode码,我需要ascii。
>>> s = "Йог" #cyrillic
>>> for char in s:
... print(ord(char))
...
1049 #unicode
1086 #unicode
1075 #unicode
我需要 ASCII。如何获得? (下)
你不能; 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) 中没有西里尔字符。为了获得此编码中字符的代码点,您需要首先将字符串编码为 CP1251,然后获取结果字节的值:
# Assuming Python 3
s = "Йог".encode('cp1251')
for b in s:
print(b)
glagolitsa = "А,Б,В,Г,Д,Е,Ё,Ж,З,И,Й,К,Л,М,Н,О,П,Р,С,Т,У,Ф,Х,Ц,Ч,Ш,Щ,Ъ,Ы,Ь,Э,Ю,Я"
Glagolitsa = glagolitsa.split(',')
Glagolitsa
for i in range(len(Glagolitsa)):
char = Glagolitsa[i]
print(ord(char))
glagolitsa = glagolitsa.lower().split(',')
for i in range(len(glagolitsa)):
char = glagolitsa[i]
print(ord(char))
for i in range(1040, 1104):
print(chr(i))