[Tutor] Conflict with encoding in console view and file dump

Peter Otten __peter__ at web.de
Tue Aug 3 18:29:33 CEST 2010

Alex Baraibar wrote:

> Hi, Peter.
> Sorry for the delay, I've been out for a while.
> So I did what you suggested and it prints the following:
> "
> import sys
> print >> sys.stderr, sys.stdout.encoding
> what does the above print
> (a) when you print to the console? it prints cp850
> (b) when you redirect to a file? it prints None

That's as expected. 

You should normally get an exception like

Traceback (most recent call last):
  File "tmp_enc.py", line 33, in <module>
    print element
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 
11: ordinal not in range(128)

when you redirect your script's output to a file.

Did you change the default encoding? If you didn't you probably have an ill-
behaved application on your system that did it without asking.
> Any suggestions?

Check stdout.encoding, and if it is None (unknown encoding) wrap the stdout 
stream into a Writer. Your script with that modification:

# -*- coding: cp1252 -*-

def festivos():
    fest = [ 'Any Nou:\t\t\t1 de enero',
             'Reis:\t\t\t\t6 de enero',
             'Festa del Treball:\t\t1 de mayo',
             'Sant Joan:\t\t\t24 de junio',
             u'La Assumpció:\t\t\t15 de agosto',
             'La Diada:\t\t\t11 de septiembre',
             u'La Mercè:\t\t\t24 de septiembre',
             'La Hispanitat:\t\t\t12 de octubre',
             'Tots Sants:\t\t\t1 de novembre',
             u'La Constitució:\t\t\t6 de desembre',
             u'La Concepció:\t\t\t8 de desembre',
             'Nadal:\t\t\t\t25 de desembre',
             'Sant Esteve:\t\t\t26 de desembre' ]
    return fest

def separador( num, char ):
    return char * num

# --- Main ---
import sys
if sys.stdout.encoding is None:
    import codecs
    Writer = codecs.getwriter("latin-1")
    sys.stdout = Writer(sys.stdout)

dias = festivos()
print "Los festivos fijos anuales son:\n"
for element in dias:
    sep = separador( 50, '-' ) # move that out of the loop
    print element
    print sep


More information about the Tutor mailing list