问题
# -*- 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?
回答1:
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:
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky
The Python Unicode HOWTO
Pragmatic Unicode by Ned Batchelder
before you continue.
回答2:
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).
来源:https://stackoverflow.com/questions/17521720/russian-in-lists-in-python-2-7