2014-01-06 79 views
3
a = ['M\xc3\xa3e'] 
b = 'M\xc3\xa3e' 
print a 
print b 

结果:打印Unicode字符列表里面

['M\xc3\xa3e'] 
Mãe 

如何打印a,如:['Mãe']

+1

通常情况下,您想要打印各个元素,而不是它们的表示。 – Matthias

+1

另请参阅:http://stackoverflow.com/questions/16798811/print-list-of-unicode-chars-without-escape-characters – Yosh

+0

@Matthias,如果是这种情况,打印'B'需要打印'M \ xc3 \ xa3e'来代替。 –

回答

1

这是在python2

但在python3你会得到一个特点是什么你要 :)。

$ python3 
Python 3.3.3 (default, Nov 26 2013, 13:33:18) 
[GCC 4.8.2] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>> a = ['M\xc3\xa3e'] 
>>> print(a) 
['Mãe'] 
>>> 

或python2您可以:

print '[' + ','.join("'" + str(x) + "'" for x in a) + ']' 
2

在python2你也可以继承list类,并使用__unicode__方法:

#Python 2.7.3 (default, Sep 26 2013, 16:38:10) 

>>> class mylist(list): 
... def __unicode__(self): 
... return '[%s]' % ', '.join(e.decode('utf-8') if isinstance(e, basestring) 
...        else str(e) for e in self) 
>>> a = mylist(['M\xc3\xa3e', 11]) 
>>> print a 
['M\xc3\xa3e', 11] 
>>> print unicode(a) 
[Mãe, 11]