Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert unicode cyrillic symbols to string in python [duplicate]

When I try to convert unicode:

a = u"Тест"

To string:

str(a)

I got this error:

'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

I need str(a) to give me output:

>> str(a)
>> 'Тест'
like image 202
UnLiMiTeD Avatar asked Oct 05 '22 07:10

UnLiMiTeD


1 Answers

Pick an encoding that can encode Cyrillic symbols, such as UTF-8:

>>> a = u'Тест'

>>> a.encode('utf-8')
'\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82'

The ASCII table doesn't have code points for Cyrillic characters, so you need to specify an encoding explicitly.

But if all you want is just print the string, then what you should care about is the encoding of your terminal and the system font.

like image 164
Lev Levitsky Avatar answered Oct 13 '22 12:10

Lev Levitsky