Unicode in MIMEText

Steve Holden steve at holdenweb.com
Thu Nov 24 01:02:54 EST 2005


Damjan wrote:
>>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
> 
> 
> 
> 
> 
... and being concerned to improve the library you logged this patch in 
Sourceforge for consideration by the developers?

That's the only way to guarantee proper consideration of your fix.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list