Unicode - and MIMEType - Good friday fun.

MRAB python at mrabarnett.plus.com
Fri Sep 11 20:01:00 EDT 2009


rh0dium wrote:
> Hi Geniuses,
> 
> Can anyone please show me the way..  I don't understand why this
> doesn't work...
> 
> 
> # encoding: utf-8
> from email.MIMEText import MIMEText
> 
> msg = MIMEText("hi")
> msg.set_charset('utf-8')
> print msg.as_string()
> 
> a = 'Ho\xcc\x82tel Ste\xcc\x81phane '
> b = unicode(a, "utf-8")
> 
> print b
> 
> msg = MIMEText(b)
> msg.set_charset('utf-8')
> print msg.as_string()
> 
> It should right??
> 
'b' is Unicode, but you're telling 'msg' that it's UTF-8, which it
isn't. Try giving 'msg' the UTF-8 string:

 >>> msg = MIMEText(a)
 >>> msg.set_charset('utf-8')
 >>> print msg.as_string()
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="utf-8"

Hôtel Stéphane



More information about the Python-list mailing list