Print LIST of unicode chars without escape characters

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

>>> s = "äåö"
>>> s
'xc3xa4xc3xa5xc3xb6'
>>> print s
äåö

but if we have a list containing the string above and print it:

>>> s = ['äåö']
>>> s
['xc3xa4xc3xa5xc3xb6']
>>> print s
['xc3xa4xc3xa5xc3xb6']

You still get escaped character sequences. How do you go about to get the content of the list unescaped, is it possible? Like this:

>>> print s
['äåö']

Also, if the strings are of the unicode type, how do you go about doing the same as above?

>>> s = u'åäö'
>>> s
u'xe5xe4xf6'
>>> print s
åäö
>>> s = [u'åäö']
>>> s
[u'xe5xe4xf6']
>>> print s
[u'xe5xe4xf6']

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

Is this satisfactory?

>>> s = ['äåö', 'äå']
>>> print "n".join(s)
äåö
äå
>>> print ", ".join(s)
äåö, äå


>>> s = [u'åäö']
>>> print ",".join(s)
åäö

Method 3

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

>>> s = ['äåö']
>>> s
['xc3xa4xc3xa5xc3xb6']

In Python 3, however, it displays properly:

>>> s = ['äåö']
>>> s
['äåö']

Method 4

Another solution

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

gives
[äåö, äå]

Method 5

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:
# ['xd0x92xd0xb0xd0xbbxd0xb5xd0xbdxd0xbaxd0xb8', 'xd0x9cxd0xb0xd1x82xd1x80xd1x91xd1x88xd0xbaxd0xb0']
print ReprToStr(russian1)
# Output:
# ['Валенки', 'Матрёшка']


russian2 = {'Валенки': 145, 'Матрёшка': 100500}
print russian2
# Output:
# {'xd0x92xd0xb0xd0xbbxd0xb5xd0xbdxd0xbaxd0xb8': 145, 'xd0x9cxd0xb0xd1x82xd1x80xd1x91xd1x88xd0xbaxd0xb0': 100500}
print ReprToStr(russian2)
# Output:
# {'Матрёшка': 100500, 'Валенки': 145}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x