Unicode in MIMEText

Damjan gdamjan at gmail.com
Wed Nov 23 22:24:38 EST 2005


> Why doesn't this work:
> 
> from email.MIMEText import MIMEText
> msg = MIMEText(u'\u043a\u0438\u0440\u0438\u043b\u0438\u0446\u0430')
> msg.set_charset('utf-8')
> msg.as_string()
...
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7:
> ordinal not in range(128)

It's a real shame that unicode support in the python library is very weak
sometimes...

Anyway I solved my problem by patching email.Charset

--- Charset.py~ 2005-11-24 04:20:09.000000000 +0100
+++ Charset.py  2005-11-24 04:21:02.000000000 +0100
@@ -244,6 +244,8 @@
         """Convert a string from the input_codec to the output_codec."""
         if self.input_codec <> self.output_codec:
             return unicode(s, self.input_codec).encode(self.output_codec)
+        elif isinstance(s, unicode):
+            return s.encode(self.output_codec)
         else:
             return s





-- 
damjan



More information about the Python-list mailing list