[Tutor] converting encoded symbols from rss feed?

Kent Johnson kent37 at tds.net
Fri Jun 19 04:53:03 CEST 2009


On Thu, Jun 18, 2009 at 9:03 PM, Serdar Tumgoren<zstumgoren at gmail.com> wrote:

> When I run this code:
>
> <<< snip >>>
> for line in infile:
>    cleanline = translate_code(line)
>    newline = strip_html(cleanline)
>    outfile.write(newline)
> <<< snip >>>
>
> ...I receive the below traceback:
>
>   Traceback (most recent call last):
>      File "htmlcleanup.py", line 112, in <module>
>      outfile.write(newline)
>   UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in
> position 21: ordinal not in range(128)

OK, so newline is unicode, outfile.write() wants a plain string. What
encoding do you want outfile to be in? Try something like
outfile.write(newline.encode('utf-8'))
or use the codecs module to create an output that knows how to encode.

Kent


More information about the Tutor mailing list