Python + Cyrillic?

Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
Situation: there is a console application written in Python (version 2.7). During the work the application should display the (reading not necessary, I do not know if it is important) the data to the console, including the text in Russian. The problem

def __str__(self): return "%s |(%s)|" % (,

here — a string, which can be Russian characters.

Without any conversions at all — there is a bug:

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

If you replace the code to:

def __str__(self): return "%s |(%s)|" % (repr("UTF-8")),

the exclusion does not crashes, but the console are not displayed Cyrillic characters and their byte representation.


'\\xd0\\x95\\xd0\\xbb\\xd0\\xb5\\xd0\\xbd\\xd0\\xb0 S.' |(157927927)|

Instead of:

Elena S.

How to solve this problem? Latin is no problem, the same code outputs is not a byte representation, and the normal Latin characters.

That way you can read about this?
by | 3 views

4 Answers

0 like 0 dislike
#!/usr/bin/env python # -*- coding: utf-8 -*- class Foo(object): def __init__(self, name): = name def __str__(self): return 'str: %s' % def __unicode__(self): return 'uni: %s' %'utf-8') def __repr__(self): return 'repr: %s' % a = 'Elena's' b = Foo(a) print(str(b)) print(unicode(b)) print(repr(b)) 

str: Elena S
uni: Elena S
repr: Elena S
0 like 0 dislike
but if a = u Elena S'?
0 like 0 dislike
Dear author !
I've been thinking it!
until I read your article
versin python 2
-*- coding: utf-8 -*-
print 'word'
alas, he did not support modules language support !

but my problem was that while working with eclipse mars all the words output by the program, he frantically showed manifested as a the error and given number of code memory cells storing this incomprehensible to the interpreter object .
helped that!
sudo apt-get install python 3
and of course the change of compiler python 2 to a later version !
0 like 0 dislike
Suddenly someone come in handy - to pyton2 did not swear you need at the beginning of class to register
@python_2_unicode_compatible class AuthorModel(models.Model):

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
28,070 users