locale.CODESET / different in python shell and scripts

Nuff Said nuffsaid at phreaker.net
Wed Apr 28 18:37:39 EDT 2004


On Tue, 27 Apr 2004 22:29:59 +0200, Martin v. Löwis wrote:
> Because, for some reason, locale.setlocale() is called in your
> interactive startup, but not in the normal startup.
> 
> It is uncertain why this happens - setlocale is not normally
> called automatically; not even in interactive mode. Perhaps
> you have created your own startup file?

I use two Python versions on my Linux box (Fedora Core 1): 
the Python 2.2 which came with Fedora and a Python 2.3 which
I compiled myself. (I didn't tinker with the last one; 
Fedora's Python is a (well known) mess.)

Both Python versions give me 'ANSI_X3.4-1968' when I run a script
with 'print locale.nl_langinfo(locale.CODESET)'. 
  When I execute the same command in an interactive Python shell, 
I get the (correct) 'UTF-8'. 

(By 'correct', I mean that the bash command 'locale' gives me
'LANG=en_US.UTF-8, LC_CTYPE="en_US.UTF-8", ...'. This seems to
be correct, because e.g. the 'less ...' command shows files which
are UTF-8 encoded in the correct way; files which are e.g.
'ISO-8859-1' encoded are not shown in the correct way.)


Things are getting even worse:

I write a Python script which uses Unicode strings; now I want
to 'print ...' one of those strings (containing non-ASCII characters;
e.g. German umlauts). 
  With Fedora's Python 2.2 I have to use 'print s.encode('ISO-8859-1') 
or something similar.
  With my self-compiled Python 2.3, I have to use (the expected) 
'print s.encode('UTF-8')' (though it shows me 'ANSI_X3.4-1968' when
using 'print locale.nl_langinfo(locale.CODESET)' in the same file).

???

Any ideas what's going wrong here?

(I tried 'python -S ...'; doesn't make a difference.)




More information about the Python-list mailing list