<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Unicodedecodeerror on Tony Andrew Meyer</title><link>http://tonyandrewmeyer.com/tags/unicodedecodeerror/</link><description>Recent content in Unicodedecodeerror on Tony Andrew Meyer</description><generator>Hugo</generator><language>en-nz</language><lastBuildDate>Sun, 30 Aug 2009 21:35:44 +1200</lastBuildDate><atom:link href="http://tonyandrewmeyer.com/tags/unicodedecodeerror/index.xml" rel="self" type="application/rss+xml"/><item><title>Mailbox flattening messages</title><link>http://tonyandrewmeyer.com/2009/08/30/mailbox-flattening-messages/</link><pubDate>Sun, 30 Aug 2009 21:35:44 +1200</pubDate><guid>http://tonyandrewmeyer.com/2009/08/30/mailbox-flattening-messages/</guid><description>&lt;p>The &lt;a href="http://docs.python.org/library/mailbox.html">mailbox.py&lt;/a> code does this to dump a message (e.g. when adding to a Maildir file):&lt;/p>
&lt;pre tabindex="0">&lt;code>def _dump_message(self, message, target, mangle_from_=False):
 # Most files are opened in binary mode to allow predictable seeking.
 # To get native line endings on disk, the user-friendly \n line endings
 # used in strings and by email.Message are translated here.
 &amp;#34;&amp;#34;&amp;#34;Dump message contents to target file.&amp;#34;&amp;#34;&amp;#34;
 if isinstance(message, email.Message.Message):
 buffer = StringIO.StringIO()
 gen = email.Generator.Generator(buffer, mangle_from_, 0)
 gen.flatten(message)
 buffer.seek(0)
 target.write(buffer.read().replace(&amp;#39;\n&amp;#39;, os.linesep))
 elif isinstance(message, str):
 if mangle_from_:
 message = message.replace(&amp;#39;\nFrom &amp;#39;, &amp;#39;\n&amp;gt;From &amp;#39;)
 message = message.replace(&amp;#39;\n&amp;#39;, os.linesep)
 target.write(message)
&lt;/code>&lt;/pre>&lt;p>(There&amp;rsquo;s a bit more that deals with other types of message).  Unfortunately, with some messages containing Unicode characters, this breaks with a UnicodeDecodeError:&lt;/p></description></item></channel></rss>