Getting ASCII encoding where unicode wanted under Py3k

MRAB python at mrabarnett.plus.com
Mon May 13 12:34:15 EDT 2013


On 13/05/2013 16:59, Jonathan Hayward wrote:
> I have a Py3k script, pasted below. When I run it I get an error about
> ASCII codecs that can't handle byte values that are too high.
>
> The error that I am getting is:
>
> |UnicodeEncodeError:  'ascii'  codec can't encode character'\u0161' in position 1442: ordinal not in range(128)
>        args = ('ascii', "Content-Type: text/html\n\n<!DOCTYPE html>\n<html>\n...ype='submit'>\n </form>\n </body>\n</html>", 1442, 1443,'ordinalnot  in  range(128)')
>        encoding = 'ascii'
>        end = 1443
>        object = "Content-Type: text/html\n\n<!DOCTYPE html>\n<html>\n...ype='submit'>\n </form>\n </body>\n</html>"
>        reason = 'ordinalnot  in  range(128)'
>        start = 1442
>        with_traceback = <built-in method with_traceback of UnicodeEncodeError object>|
>
> (And that was posted to StackOverflow--one shot in the dark answer so far.)
>
> My code is below. What should I be doing differently to be, in the most
> immediate sense, calls to '''%(foo)s''' % locals()?
>
[snip]
The 'print' functions send its output to sys.stdout, which, in your
case, is set up to encode to ASCII for output, but '\u0161' can't be
encoded to ASCII.

Try encoding to UTF-8 instead:

from codecs import getwriter

sys.stdout = getwriter("utf-8")(sys.stdout.buffer)




More information about the Python-list mailing list