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


Groups > comp.lang.python > #100089

Re: writing an email.message.Message in UTF-8

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Adam Funk <a24061@ducksburg.com>
Newsgroups comp.lang.python
Subject Re: writing an email.message.Message in UTF-8
Date Mon, 07 Dec 2015 15:21:40 +0000
Organization $CABAL
Lines 40
Message-ID <4pkgjcxr0j.ln2@news.ducksburg.com> (permalink)
References <nbjgjcxqsh.ln2@news.ducksburg.com>
X-Trace individual.net OlG2ayk4n4tzihlEHncg+gMGr1fhfJ/sIonIJMIptBEgLOPnVO
X-Orig-Path news.ducksburg.com!not-for-mail
Cancel-Lock sha1:mlYInwoAr2zTKEK5ET2cUkB9UCY= sha1:x/xSbHiOI+afrxgBj9Qh/U3gkw4=
User-Agent slrn/pre1.0.3-5 (Linux)
Xref csiph.com comp.lang.python:100089

Show key headers only | View raw


On 2015-12-07, Adam Funk wrote:

> 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')

I've also tried changing that to
     reply.set_payload('\n'.join(body_lines) + '\n', 'utf-8')
but I get the same error on output.

>     ...
>     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?
>
> Thanks.
>
>


-- 
Cats don't have friends.  They have co-conspirators.
         http://www.gocomics.com/getfuzzy/2015/05/31

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


Thread

writing an email.message.Message in UTF-8 Adam Funk <a24061@ducksburg.com> - 2015-12-07 14:57 +0000
  Re: writing an email.message.Message in UTF-8 Adam Funk <a24061@ducksburg.com> - 2015-12-07 15:21 +0000
  Re: writing an email.message.Message in UTF-8 Terry Reedy <tjreedy@udel.edu> - 2015-12-07 12:40 -0500
    Re: writing an email.message.Message in UTF-8 Adam Funk <a24061@ducksburg.com> - 2015-12-08 09:35 +0000
  Re: writing an email.message.Message in UTF-8 dieter <dieter@handshake.de> - 2015-12-08 08:50 +0100
    Re: writing an email.message.Message in UTF-8 Adam Funk <a24061@ducksburg.com> - 2015-12-08 09:35 +0000

csiph-web