Strings show as brackets with a 'u'.

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jul 27 06:20:39 CEST 2011


On Wed, 27 Jul 2011 11:18 am goldtech wrote:

> Thank you. So what is happening? Is it that I'm in an environment that
> is not unicode and python is telling me the string (ie. n[0]) is
> unicode? (I'll do a hatchet-job on this subject if I ask anymore about
> unicode). Thanks to posters for their patience!

It's hard to say with no context remaining. I take it you're printing n[0]
and getting unexpected results?

To start with, try calling this: type(n[0])

That will tell you if the object is a byte-string, or unicode.

I don't think there is any way to tell the console's encoding directly from
Python, although you can look at sys.stdout.encoding which will tell you
what Python thinks it is.

I think that from Windows you can run chcp.exe to see the console encoding,
although I can't confirm that. From Linux, there's probably a bazillion
different ways, none of which guaranteed to be either correct or consistent
with the others. But I'm not bitter.

You can try with the default locale encoding, or the LANG environment
variable:

locale charmap
echo $LANG

but that only tells you what your shell thinks the encoding should be, not
what your terminal is actually using.

Your console app (xterm, konsole, Gnome terminal, whatever...) probably has
a way to query what encoding it is using, but I'll be buggered if I can
find out what that is. In konsole I can look at the Settings > Encodings
menu, but it claims to be using "default". How very useful.



-- 
Steven




More information about the Python-list mailing list