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

skip at pobox.com skip at pobox.com
Thu Feb 14 22:09:05 CET 2008


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


More information about the Python-3000 mailing list