0

On Console:

>>> print {"key": ["äüö"]}
{'key': ['\xc3\xa4\xc3\xbc\xc3\xb6']}

How can I easily let python print something like this:

>>> print {"key": ["äüö"]}
{'key': ['äüö']}

I don't like to print unicode characters like in How to print Unicode character in Python? I like to have an easy way to print the content of a dictionary.

C-Jay
  • 621
  • 1
  • 11
  • 22

1 Answers1

2

When you print a collection with Python 2, for instance a dict or a list, Python uses the repr() function to print the items of the collections.

In the case of a string (unicode string) you get escaped characters…

To do what you want, using Python 2, you need to print the dictionary yourself, like this:

>>> d = {"key": [u"äüö"]}
>>> for k, v in d.iteritems():
...     print(u"{k}: [{v}]".format(k=k, v=u", ".join(u"'{0}'".format(i) for i in v)))

You get:

key: ['äüö']
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Ok, nice solution. But what if the collection is more complex. E.g more nested dictionaries with nested lists? So I have to write a more complex parser. – C-Jay Dec 20 '17 at 22:19