a = ['M\xc3\xa3e']
b = 'M\xc3\xa3e'
print a
print b
results:
['M\xc3\xa3e']
Mãe
How can I print a like: ['Mãe']
In python2 you can subclass list class and use __unicode__ method:
#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]
This is a feature in python2
But in python3 you will get what you want :).
$ 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']
>>>
or in python2 you can:
print '[' + ','.join("'" + str(x) + "'" for x in a) + ']'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With