The python print function treats unicode differently than when Dive Into Python is written?

I am trying to overcome some uncomfortable encoding issues by returning to the basics. In the Dive Into Python 9.14 example ( here ) we have the following:

>>> s = u'La Pe\xf1a' >>> print s Traceback (innermost last): File "<interactive input>", line 1, in ? UnicodeError: ASCII encoding error: ordinal not in range(128) >>> print s.encode('latin-1') La Peña 

But on my machine this happens:

 >>> sys.getdefaultencoding() 'ascii' >>> s = u'La Pe\xf1a' >>> print s La Peña 

I don’t understand why they are different. Is anyone

+4
source share
1 answer

The default encoding for print not dependent on sys.getdefaultencoding() , but on sys.stdout.encoding . If you run python using, for example, LANG=C or redirect the python script to a file, the encoding for stdout will be ANSI_X3.4-1968 . On the other hand, if sys.stdout is a terminal, it will use terminal encoding.

Explain what sys.getdefaultencoding() does - it is used when implicitly converting strings from / to unicode. In this example, str(u'La Pe\xf1a') with the default ASCII encoding will fail, but with the changed default encoding, it will encode the string in Latin-1. However, setting the default encoding is a terrible idea, you should always use explicit encoding if you want to switch from unicode to str .

+6
source

All Articles