Unicode - and MIMEType - Good friday fun.

Discussion in 'Python' started by rh0dium, Sep 12, 2009.

  1. rh0dium

    rh0dium Guest

    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!
    rh0dium, Sep 12, 2009
    #1
    1. Advertising

  2. rh0dium

    MRAB Guest

    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"

    HoÌ‚tel SteÌphane
    MRAB, Sep 12, 2009
    #2
    1. Advertising

  3. > 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 <> 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)
    Rami Chowdhury, Sep 12, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGFuaWVsIENoYWl0?=

    HtmlInputFile: mimetype of image/jpeg vs image/pjpeg?

    =?Utf-8?B?RGFuaWVsIENoYWl0?=, Oct 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    924
    =?Utf-8?B?RGFuaWVsIENoYWl0?=
    Oct 20, 2004
  2. Andy Fish
    Replies:
    65
    Views:
    1,702
    Mabden
    May 18, 2004
  3. forge1968
    Replies:
    0
    Views:
    497
    forge1968
    Apr 26, 2007
  4. dolphin
    Replies:
    4
    Views:
    312
    Jorgen Grahn
    Aug 25, 2007
  5. er
    Replies:
    2
    Views:
    487
Loading...

Share This Page