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


Groups > comp.lang.python > #100139

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From dieter <dieter@handshake.de>
Newsgroups comp.lang.python
Subject Re: writing an email.message.Message in UTF-8
Date Tue, 08 Dec 2015 08:50:07 +0100
Lines 32
Message-ID <mailman.52.1449561032.12405.python-list@python.org> (permalink)
References <nbjgjcxqsh.ln2@news.ducksburg.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace news.uni-berlin.de kN0LGPe5HWNwn7ZBWrwC9Aagv2yVbUaLADyvUkIoZlZw==
Cancel-Lock sha1:MsdrSWhvq02CurC4jM7upu3Bkw8=
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(python': 0.05; 'utf-8': 0.07; "'\\n')": 0.09; "'w',": 0.09; 'encode': 0.09; 'output?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'thrown': 0.09; 'exception': 0.13; 'headers': 0.15; '2.7.3': 0.16; 'adam': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:UTF': 0.16; 'subject:writing': 0.16; 'skip:" 40': 0.20; 'file.': 0.22; 'pass': 0.22; 'trying': 0.22; 'replacing': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:e 30': 0.27; 'closer': 0.29; "i'm": 0.30; 'says': 0.32; "skip:' 20": 0.34; 'could': 0.35; 'i.e.': 0.35; 'instance': 0.35; 'replace': 0.35; 'unicode': 0.35; 'but': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset :us-ascii': 0.37; 'subject:-': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'body': 0.61; 'better.': 0.66; 'received:217': 0.66; 'reply': 0.68; 'payload': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host pd9e09fef.dip0.t-ipconnect.de
User-Agent Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100139

Show key headers only | View raw


Adam Funk <a24061@ducksburg.com> 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.

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