Skip to content Skip to sidebar Skip to footer

How To Prevent Str To Encode Unicode Characters As Hex Codes?

When I print a unicode string in Python directly, I see a string with same characters that I have in my string. When I embed it into some container (put in a list, in a map, etc),

Solution 1:

print [v] calls repr(v) that returns ascii-printable characters as is and everything else is escaped using \x, \u, \U, ...

Remember an object such as dict(a=1) is different from its text representation (repr(dict(a=1))). Unicode string is an object too (type(v) == unicode) like any other and therefore repr(v) is not v (btw, repr(repr(v)) is not repr(v) too -- think about it).

To display human-readable text for debugging in Python console, you could provide custom sys.displayhook e.g., you could encode any (embedded) unicode object using sys.stdout.encoding. In Python 3, repr(unicode_string) returns Unicode characters that are printable in the current environment as is (characters that would cause UnicodeEncodeError are escaped).

str(v) raising UnicodeEncodeError is unrelated. str(v) calls v.encode(sys.getdefaultencoding()) and therefore it fails for any unicode string with non-ascii characters. Do not call str() on Unicode strings (it is almost always an error), print Unicode directly instead.

Solution 2:

Don't change str, change your way of thinking.

If you need to print netsted element than get it from container and print it - don't print all container.

v = u"abc123абв"
d = [v, v, v]

print d[0]
# abc123абвprint", ".join(d)
# abc123абв, abc123абв, abc123абв

btw: Python print hex code (and other elements) for testing/debuging reason.

When you see

[u'abc123\u0430\u0431\u0432']

you know: it is list ([ and ]) with unicode text (u and ') and there are non-ASCII chars in that text.

Post a Comment for "How To Prevent Str To Encode Unicode Characters As Hex Codes?"