Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unicode - and MIMEType - Good friday fun.

Reply
Thread Tools

Unicode - and MIMEType - Good friday fun.

 
 
rh0dium
Guest
Posts: n/a
 
      09-11-2009
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??

Thanks!
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      09-12-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
 
Reply With Quote
 
 
 
 
Rami Chowdhury
Guest
Posts: n/a
 
      09-12-2009
> b = unicode(a, "utf-8")

[snip]

> msg = MIMEText(b)


I believe this is the problem line -- the MIMEText constructor takes
encoded strings rather than unicode objects. Try:

msg = MIMEText(a)

Or, alternatively

msg = MIMEText(b.encode('utf-8'))

On Fri, 11 Sep 2009 16:33:42 -0700, rh0dium <(E-Mail Removed)> 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??
>
> Thanks!




--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mimetype .aspx? =?Utf-8?B?UGF0cmljayBG?= ASP .Net 2 06-30-2007 12:25 PM
VirtualPathProvider and mimetype (for RESTful applications) forge1968 ASP .Net 0 04-26-2007 03:42 AM
HtmlInputFile: mimetype of image/jpeg vs image/pjpeg? =?Utf-8?B?RGFuaWVsIENoYWl0?= ASP .Net 0 10-20-2004 12:15 PM
Problem with mimetype? Martin Honnen XML 3 06-26-2004 08:45 AM
getting mimetype of a file Ryan Paul Python 4 06-02-2004 01:21 PM



Advertisments