ActiveState Code

Recipe 439148: repr() of containers with non-ASCII strings


Printing sequences or maps containing non-ASCII strings results in escape sequences. This function uses the not-so-commonly-known "string_escape" codec to facilitate printing such sequences for quick-viewing.

Python
1
2
def decoded_repr(obj):
    return repr(obj).decode("string_escape")

Discussion

This is mainly used for quick debugging purposes. It will work for strings encoded in your environment's natural encoding, but it won't correctly show unicode characters that cannot be encoded in your natural encoding.

Sign in to comment