7bit to 8bit

Peter Hansen peter at engcorp.com
Thu Apr 15 10:09:09 EDT 2004


Daniel Roth wrote:

> When I try your 'line' I get:
> # python
> Python 2.3.3 (#2, Apr 15 2004, 04:41:13)
> [GCC 3.3.3 20040110 (prerelease) (Debian)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> print "=F6=E4".decode('quopri').decode('latin-1')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 
> 0-1: ordinal not in range(128)
>  >>>

Note that the error is coming from the attempt to print, not from
the conversion.

Do this instead to prove that what Mark gave you works:

 >>> s = "=F6=E4".decode('quopri').decode('latin-1')
 >>> print s
(here you'll get the same error as before)
 >>> s
u'\xf6\xe4'

You can see that the string was converted properly (to a unicode
string) but when you try to print it, because your terminal's
encoding is ascii, it cannot be displayed properly.

-Peter



More information about the Python-list mailing list