Reputation: 1043
Let's say I have a content in Russian in a variable:
msg = '<some russian text here>'
print msg
gives me correct value but
print [msg]
gives me this:
['\xd0\x9f\xd0\xa4 "\xd0\x9a\xd0\xa2\xd0\x9f-\xd0\xa3\xd1\x80\xd0\xb0\xd0\xbb" (\xd0\x97\xd0\x90\xd0\x9e)']
How do I keep cyrillic symbols in list?
Upvotes: 1
Views: 1572
Reputation: 11781
You cannot do that directly, but you can get very close with pprint
.
There is example code in https://stackoverflow.com/a/10883893/705086
It covers unicode type only, but can easily be adapted to utf-8 encoded str/bytes as in OP.
Ideally pprint should maintain the invariant that formatted/printed PDO is a valid Python expression. Linked code can be hacked to maintain this invariant just as well.
You can monkey-path pprint
module to maintain this invariant:
import functools, pprint
def escape(s):
lead = ""
if isinstance(s, unicode):
s = s.encode("utf-8")
lead = "u"
return "%s\"%s\"" % (lead, s.replace("\\", "\\\\").replace("\"", "\\\""))
def patched(f):
if hasattr(f, "_already_patched"):
return f
@functools.wraps(f)
def sub(object, *args, **kwargs):
try:
if isinstance(object, basestring):
return escape(object), True, False
except Exception:
pass
return f(object, *args, **kwargs)
sub._already_patched = True
return sub
pprint._safe_repr = patched(pprint._safe_repr)
pprint.pprint([u"\N{EURO SIGN}", u"\N{EURO SIGN}".encode("utf-8")])
[u"€", "€"]
Upvotes: 1