33

If I've got a dictionary of the form:

a = {u"foo": u"ბარ"}

and I write

>>> print a[u"foo"]

I get

ბარ

as expected. But if I write

>>> print a

I get

{u'foo': u'\u10d1\u10d0\u10e0'}, but I would prefer the characters themselves to be printed.

All the data will ultimately get dumped into a database anyway, so it's not critical to solve this problem, but for debugging it would be nice if I could get readable output when I print the entire dictionary. Is there any way to do this?

For those who are curious, the script is Georgian, and yes, it says "bar".

Chrest
  • 776
  • 1
  • 9
  • 11

2 Answers2

74

This works in my terminal:

print repr(a).decode("unicode-escape")
Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • i'm getting wrongs strings, for example "indeterminação" instead "indeterminação". – ademar111190 Aug 04 '13 at 00:07
  • 2
    @ademar111190 : Looks like it's trying to use UTF-8, but your terminal is displaying it in a single-byte encoding, like cp1252. Try `print repr(a).decode('unicode-escape').encode('latin-1')`. – Thomas K Aug 04 '13 at 09:22
0

Thomas answer is absolutely correct. I just want to add this line for everyone who wants a more readable output:

print json.dumps(a, indent=4, default=str).decode('unicode-escape')
Jahan
  • 363
  • 6
  • 16