Russian in lists in python 2.7

戏子无情 提交于 2020-03-04 07:39:08

问题


# -*- 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!