Print LIST of unicode chars without escape characters

后端 未结 5 860
南方客
南方客 2020-11-27 07:39

If you have a string as below, with unicode chars, you can print it, and get the unescaped version:

>>> s = \"äåö\"
>>> s
\'\\xc3\\xa4\\xc3         


        
相关标签:
5条回答
  • 2020-11-27 08:10

    Is this satisfactory?

    >>> s = ['äåö', 'äå']
    >>> print "\n".join(s)
    äåö
    äå
    >>> print ", ".join(s)
    äåö, äå
    
    
    >>> s = [u'åäö']
    >>> print ",".join(s)
    åäö
    
    0 讨论(0)
  • 2020-11-27 08:12

    Another solution

    s = ['äåö', 'äå']
    encodedlist=', '.join(map(unicode, s))
    print(u'[{}]'.format(encodedlist).encode('UTF-8'))
    

    gives [äåö, äå]

    0 讨论(0)
  • 2020-11-27 08:14

    In Python 2.x the default is what you're experiencing:

    >>> s = ['äåö']
    >>> s
    ['\xc3\xa4\xc3\xa5\xc3\xb6']
    

    In Python 3, however, it displays properly:

    >>> s = ['äåö']
    >>> s
    ['äåö']
    
    0 讨论(0)
  • 2020-11-27 08:16

    When you print a string, you get the output of the __str__ method of the object - in this case the string without quotes. The __str__ method of a list is different, it creates a string containing the opening and closing [] and the string produced by the __repr__ method of each object contained within. What you're seeing is the difference between __str__ and __repr__.

    You can build your own string instead:

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

    This version should work on both Unicode and byte strings in Python 2:

    print u'[' + u','.join(u"'" + unicode(x) + u"'" for x in s) + u']'
    
    0 讨论(0)
  • 2020-11-27 08:24

    One can use this wrapper class:

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    class ReprToStrString(str):
        def __repr__(self):
            return "'" + self.__str__() + "'"
    
    
    class ReprToStr(object):
        def __init__(self, printable):
            if isinstance(printable, str):
                self._printable = ReprToStrString(printable)
            elif isinstance(printable, list):
                self._printable = list([ReprToStr(item) for item in printable])
            elif isinstance(printable, dict):
                self._printable = dict(
                    [(ReprToStr(key), ReprToStr(value)) for (key, value) in printable.items()])
            else:
                self._printable = printable
    
        def __repr__(self):
            return self._printable.__repr__()
    
    
    russian1 = ['Валенки', 'Матрёшка']
    print russian1
    # Output:
    # ['\xd0\x92\xd0\xb0\xd0\xbb\xd0\xb5\xd0\xbd\xd0\xba\xd0\xb8', '\xd0\x9c\xd0\xb0\xd1\x82\xd1\x80\xd1\x91\xd1\x88\xd0\xba\xd0\xb0']
    print ReprToStr(russian1)
    # Output:
    # ['Валенки', 'Матрёшка']
    
    
    russian2 = {'Валенки': 145, 'Матрёшка': 100500}
    print russian2
    # Output:
    # {'\xd0\x92\xd0\xb0\xd0\xbb\xd0\xb5\xd0\xbd\xd0\xba\xd0\xb8': 145, '\xd0\x9c\xd0\xb0\xd1\x82\xd1\x80\xd1\x91\xd1\x88\xd0\xba\xd0\xb0': 100500}
    print ReprToStr(russian2)
    # Output:
    # {'Матрёшка': 100500, 'Валенки': 145}
    
    0 讨论(0)
提交回复
热议问题