Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KOI8-R: Having trouble translating a string

This Python script gets translit for Russian letters:

s = u'Код Обмена Информацией, 8 бит'.encode('koi8-r')
print ''.join([chr(ord(c) & 0x7F) for c in s]) # kOD oBMENA iNFORMACIEJ, 8 BIT

That works. But I want to modify it so as to get user input. Now I'm stuck at this:

s = raw_input("Enter a string you want to translit: ")

s = unicode(s)
s = s.encode('koi8-r')

print ''.join([chr(ord(c) & 0x7F) for c in s])

Ending up with this:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

What's wrong?

like image 995
user122922 Avatar asked Oct 26 '25 16:10

user122922


1 Answers

s = unicode(s) expects ascii encoding by default. You need to supply it an encoding your input is in, e.g. s = unicode(s, 'utf-8').

like image 93
laalto Avatar answered Oct 29 '25 06:10

laalto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!