[Tutor] changing unicode to ascii

Hugo Arts hugo.yoshi at gmail.com
Wed Jan 30 16:32:48 CET 2013


On Wed, Jan 30, 2013 at 2:39 PM, Benjamin Fishbein <bfishbein79 at gmail.com>wrote:

> I was trying to write text to a file and got the following error:
>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in
> position 5495: ordinal not in range(128)
>
> I tried several things I found online, such as:
>
> text.encode("ascii", "ignore")
>
> which gave me the same error
>
> and
> text.replace("\xa0", "")
>
> again, UnicodeEncodeError
>
> I tried
> text.decode("ascii", "ignore")
> and got the same result.
>
> I'm using Python 2.7, so there's some special technique for this version?
> I think the utf-8 data is unnecessary; I can strip it if I can't convert
> it to ascii.
> Of course, I can't figure out how to strip it either.
> Any help is much appreciated.
> Thanks,
> Ben
>
>
Please show us the relevant parts of your code so we can properly diagnose
the issue. Using text.encode("ascii", "ignore") should absolutely work. You
could also just write the file in UTF-8 if you don't absolutely need ascii
by using text.encode("utf-8").

Hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130130/0589d195/attachment-0001.html>


More information about the Tutor mailing list