Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python print unicode strings in arrays as characters, not code points

Tags:

python

unicode

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".

like image 935
Chrest Avatar asked Apr 13 '11 11:04

Chrest


People also ask

How do I print Unicode value of a string in Python?

To include Unicode characters in your Python source code, you can use Unicode escape characters in the form \u0123 in your string. In Python 2. x, you also need to prefix the string literal with 'u'.

How do you escape a Unicode character in Python?

Unicode Literals in Python Source Code Specific code points can be written using the \u escape sequence, which is followed by four hex digits giving the code point. The \U escape sequence is similar, but expects 8 hex digits, not 4.

How do I fix encoding in Python?

The best way to attack the problem, as with many things in Python, is to be explicit. That means that every string that your code handles needs to be clearly treated as either Unicode or a byte sequence. The most systematic way to accomplish this is to make your code into a Unicode-only clean room.

How do you Unicode a string in Python?

To allow working with Unicode characters, Python 2 has a unicode type which is a collection of Unicode code points (like Python 3's str type). The line ustring = u'A unicode \u018e string \xf1' creates a Unicode string with 20 characters.


1 Answers

This works in my terminal:

print repr(a).decode("unicode-escape") 
like image 145
Thomas K Avatar answered Oct 11 '22 12:10

Thomas K