ratkke
ratkke

Reputation: 469

Russian in lists in python 2.7

# -*- coding: utf-8 -*-
a='Привет'
print a
b=[]
b.append(a)
print b

Here is code, which has to print Привет

['Привет']

But when I run this code, it prints Привет

['\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82']

Unfortunately, it does not print what I need. Any suggestion how to do it?

Upvotes: 1

Views: 345

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1124170

The behaviour you see is normal. You have a UTF-8 encoded byte string, and when printing a list, Python will always show such strings as Python string literals in their most portable form.

You really want to use Unicode values, and print individual items from the list:

# -*- coding: utf-8 -*-
a = u'Привет'  # Unicode literal
print a
b = []
b.append(a)
print b[0]     # print individual item

If you don't yet know the difference between Unicode and a byte string or anything much about codecs, I urge you to read:

before you continue.

Upvotes: 8

JBernardo
JBernardo

Reputation: 33407

When printing lists, Python will use the repr function on the elements inside it.

The repr function for strings in Python 2.x will make it 100% ascii compatible to avoid problems like wrong encoding in your terminal.

You have to iterate your list and print each element.

Also, I advise you to use unicode strings for text (even more important if it is not ascii).

Upvotes: 2

Related Questions