trying to force stdout to utf-8 with errors='ignore' or 'replace'

Adam Funk a24061 at ducksburg.com
Fri Dec 11 11:54:27 EST 2015


On 2015-12-11, Peter Otten wrote:

> Adam Funk wrote:

>> but with either or both of those, I get the dreaded
>> "UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
>> 562: ordinal not in range(128)".  How can I force the output to be in
>> UTF-8 & silently suppress invalid characters?
>
> (I'm assuming you are using Python 2 and that main_body is a unicode 
> instance)

The short answer turned out to be 'switch to Python 3', which I think
is what I'll do from now on unless I absolutely need a library that
isn't available there.

(AFAICT, the email parser in 2.7 returns the body as a bytestring &
doesn't actually look at the Content-Type header, & trying to decode
the body with that just made it barf in different places.)


-- 
Science is what we understand well enough to explain to a computer.  
Art is everything else we do.                      --- Donald Knuth



More information about the Python-list mailing list