f.rodrigues
f.rodrigues

Reputation: 3587

Printing Unicode Char inside a List

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']

Upvotes: 3

Views: 921

Answers (3)

Yosh
Yosh

Reputation: 2742

For personal use, this module https://github.com/moskytw/uniout will come very handy.

Upvotes: 0

ndpu
ndpu

Reputation: 22571

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]

Upvotes: 2

atupal
atupal

Reputation: 17240

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) + ']'

Upvotes: 1

Related Questions