Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45250

Re: Getting ASCII encoding where unicode wanted under Py3k

Date 2013-05-13 17:34 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Getting ASCII encoding where unicode wanted under Py3k
References <CAE6_B5TDWrv4riQ27xQ+ENQHV=0ziryagsaXMRgNfwQGxmfDAA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1632.1368462864.3114.python-list@python.org> (permalink)

Show all headers | View raw


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)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Getting ASCII encoding where unicode wanted under Py3k MRAB <python@mrabarnett.plus.com> - 2013-05-13 17:34 +0100

csiph-web