[Python-3000] Does Py3k's print offer any unicode encoding help?

Guido van Rossum guido at python.org
Thu Feb 14 23:02:31 CET 2008


The encoding is a file property, not a print parameter.

The code that guesses the default stdout encoding isn't very good,
especially not on OSX. Suggestions are welcome.

You can hack around this for now by doing (before printing anything)

  sys.stdout._encoding = 'utf-8'

but that ought to be a temporary hack until we've figured out the
right way to set it.

--Guido

On Thu, Feb 14, 2008 at 1:09 PM,  <skip at pobox.com> wrote:
> A thread on the Python Mac sig got me to wondering if there is any magic in
>  Python 3's print function for printing Unicode.  Nope, no magic:
>
>     >>> print("\xef")
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>       File "/Users/skip/local/lib/python3.0/io.py", line 1246, in write
>         b = encoder.encode(s)
>       File "/Users/skip/local/lib/python3.0/encodings/ascii.py", line 22, in encode
>         return codecs.ascii_encode(input, self.errors)[0]
>     UnicodeEncodeError: 'ascii' codec can't encode character '\xef' in position 0: ordinal not in range(128)
>
>  Which kind of confuses me because on my system my default encoding is utf-8:
>
>     >>> sys.getdefaultencoding()
>     'utf-8'
>
>  Well, then how about an encoding arg?  Nope again:
>
>     >>> print("\xef", encoding="utf-8")
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     TypeError: 'encoding' is an invalid keyword argument for this function
>
>  help(print) doesn't offer any suggestions either.  Any chance that maybe an
>  encoding keyword arg could make it into an upcoming 3.0aN release?
>  (Or... Am I missing a simple solution to the problem?)  It seems that
>  printing Unicode ought to be easier in Python 3 than it is.
>
>  Skip
>  _______________________________________________
>  Python-3000 mailing list
>  Python-3000 at python.org
>  http://mail.python.org/mailman/listinfo/python-3000
>  Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>



-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list