a = ['M\xc3\xa3e']
b = 'M\xc3\xa3e'
print a
print b
['M\xc3\xa3e']
Mãe
如何打印a
,如:['Mãe']
a = ['M\xc3\xa3e']
b = 'M\xc3\xa3e'
print a
print b
['M\xc3\xa3e']
Mãe
如何打印a
,如:['Mãe']
這是在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) + ']'
在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]
對於個人使用,該模塊https://github.com/moskytw/uniout會非常方便。
通常情況下,您想要打印各個元素,而不是它們的表示。 – Matthias
另請參閱:http://stackoverflow.com/questions/16798811/print-list-of-unicode-chars-without-escape-characters – Yosh
@Matthias,如果是這種情況,打印'B'需要打印'M \ xc3 \ xa3e'來代替。 –