Printing Unicode Char inside a List

后端 未结 3 608
滥情空心
滥情空心 2020-12-11 23:21
a = [\'M\\xc3\\xa3e\']
b = \'M\\xc3\\xa3e\'
print a
print b

results:

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

How can I print

相关标签:
3条回答
  • 2020-12-11 23:56

    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]
    
    0 讨论(0)
  • 2020-12-12 00:14

    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) + ']'
    
    0 讨论(0)
  • 2020-12-12 00:14

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

    0 讨论(0)
提交回复
热议问题