Character Encodings and display of strings

Fredrik Lundh fredrik at pythonware.com
Mon Nov 13 09:39:44 EST 2006


"JKPeck" wrote:

>I am trying to understand why, with nonwestern strings, I sometimes get
> a hex display and sometimes get the string printed as characters.
>
> With my Python locale set to Japanese and with or without a # coding of
> cp932 (this is Windows) at the top of the file, I read a list of
> Japanese strings into a list, say, catlis.
>
> With this code
> for item in catlis:
>       print item
> print catlis
> print " ".join(catlis)
>
> the first print (print item) displays Japanese text as characters..
> The second print (print catlis) displays a list with the double byte
> characters in hex notation.
> The third print (print " ".join(catlis)) prints a combined string of
> Japanese characters properly.
>
> According to the print documentation,
> "If an object is not a string, it is first converted to a string using
> the rules for string conversions"
>
> but the result is different with a list of strings.

a list is not a string, so it's converted to one using the standard list representation
rules -- which is to do repr() on all the items, and add brackets and commas as
necessary.

for some more tips on printing, see:

    http://effbot.org/zone/python-list.htm#printing

</F> 






More information about the Python-list mailing list