Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Adam Funk Newsgroups: comp.lang.python Subject: Re: writing an email.message.Message in UTF-8 Date: Tue, 08 Dec 2015 09:35:06 +0000 Organization: $CABAL Lines: 41 Message-ID: References: X-Trace: individual.net 6MJaQOwwmUGBtMjJz9qtngc16bViNTs9WVGODie7gDf03QHT0W X-Orig-Path: news.ducksburg.com!not-for-mail Cancel-Lock: sha1:OHE7Lja40ADowQs2a0hIeG/EJpM= sha1:+vxlws7Fc2dg4dvh3E55yiZ1qVU= User-Agent: slrn/pre1.0.3-5 (Linux) Xref: csiph.com comp.lang.python:100143 On 2015-12-08, dieter wrote: > Adam Funk writes: > >> I'm trying to write an instance of email.message.Message, whose body >> contains unicode characters, to a UTF-8 file. (Python 2.7.3 & 2.7.10 >> again.) >> >> reply = email.message.Message() >> reply.set_charset('utf-8') >> ... # set various headers >> reply.set_payload('\n'.join(body_lines) + '\n') >> ... >> outfile = codecs.open(outfilename, 'w', encoding='utf-8', errors='ignore') >> outfile.write(reply.as_string()) >> outfile.close() >> >> Then reply.as_string() barfs a UnicodeDecodeError. I look in the >> documentation, which says the generator is better. So I replace the >> outfile.write(...) line with the following: >> >> g = email.generator.Generator(outfile, mangle_from_=False) >> g.flatten(reply) >> >> which still barfs a UnicodeDecodeError. Looking closer at the first >> error, I see that the exception was in g.flatten(...) already & thrown >> up to reply.as_string(). How can I force the thing to do UTF-8 >> output? > > You could try replacing "reply.set_payload('\n'.join(body_lines) + '\n')" > by "reply.set_payload(('\n'.join(body_lines) + '\n').encode('utf-8'))", > i.e. you would not pass in a unicode payload but an "utf-8" encode > "str" payload. That didn't work (I got the same error) but switching to python 3.2 did. Thanks, though. -- A mathematical formula should never be "owned" by anybody! Mathematics belonga to God. --- Donald Knuth