the stupid encoding problem to stdout

Sérgio Monteiro Basto sergiomb at sapo.pt
Mon Jun 13 10:15:25 EDT 2011


Ian Kelly wrote:

> If you want your output to behave that way, then all you have to do is
> specify that with an explicit encode step.

ok 

>> If we want we change default for whatever we want, but without this
>> "default change" Python should not change his behavior depending on
>> output. yeah I prefer strange output for a different platform, to a
>> decode errors.
> 
> Sorry, I disagree.  If your program is going to fail, it's better that
> it fail noisily (with an error) than silently (with no notice that
> anything is wrong).

Hi, 
ok a little resume, I got the solution which is setting env with 
PYTHONIOENCODING=utf-8, which if it was a default for modern GNU Linux, was 
made me save lots of time.
My practical problem is simple like, I make a script that want run in shell 
for testing and log to a file when use with a configuration. 
Everything runs well in a shell and sometimes (later) fails when log to a 
file, with a  "UnicodeEncodeError: 'ascii' codec can't encode character 
u'\xe7' in position".
So to work in both cases (tty and files), I filled all code with string 
.encode('utf-8') to workaround, when what always I want was use  
PYTHONIOCONDIG=utf-8. I got anything in utf-8, database is in utf-8, I 
coding in utf-8, my OS is in utf-8. In last about 3 years of learning Python 
I lost many many hours to understand this problem.  
And see, I can send ascii and utf-8 to utf-8 output and never have problems, 
but if I send ascii and utf-8 to ascii files sometimes got encode errors.
So you please consider, at least on Linux, default encode to utf-8 (because 
we have less problems) or make more clear that pipe to a file is different 
to a tty and problem was in files that defaults to ascii. Or 
make the default of IOENCONDIG based on env LANG.

Anyway many thanks for your time and for help me out.
I don't know how run the things in Python 3 , in python 3 defaults are utf-8 
? 

Thanks, 
--
Sérgio M. B. 



More information about the Python-list mailing list