How To Print National Characters In List Representation?
Solution 1:
This is the canonical representation of string constants in Python which is designed to eliminate encoding issues. Actually, it's what repr()
on a string returns. List's str()
function implementation that is called when it's printed calls repr()
on its members to represent them.
The only way to output a string with non-ASCII characters as they are is to print
it or otherwise write it to a stream. See Why does Python print unicode characters when the default encoding is ASCII? on how character conversion is done on printing. Also note that for non-ASCII 8-bit characters, the output will be different for terminals set up for different codepages.
Regarding the solution:
The simplest one will be to make an alternative str(list)
implementation that will call str()
instead of repr()
- noting the warnings above.
deflist_nativechars(l):
assertisinstance(l,list)
return"[" + ", ".join('"'+str(i)+'"'for i in l) + "]"
Now (in cp866
console encoding):
>>>l=["йцукен"]>>>print list_nativechars(l)
["йцукен"]
With data in foreign encoding:
# encoding: cp858
<...>
l= ['cmd.exe', '-Name=Mötley', '-Bike=Härley', '-Chef=Börk']
print list_nativechars(l)
c:\>python t.py
["cmd.exe", "-Name=MФtley", "-Bike=HДrley", "-Chef=BФrk"]
Post a Comment for "How To Print National Characters In List Representation?"