html codes

Discussion in 'Python' started by Daniel Fetchinson, Dec 9, 2008.

  1. Hi folks,

    I came across a javascript library that returns all sorts of html
    codes in the cookies it sets and I need my web framework (written in
    python :)) to decode them. I'm aware of htmlentitydefs but
    htmlentitydefs.entitydefs.keys( ) are of the form '&#xxx' but this
    javascript library uses stuff like '%3A' for the ':' for example. The
    conversion is here:

    http://www.ascii.cl/htmlcodes.htm

    Is there a python package/module/whatever that does the conversion for
    me or do I have to write a little wrapper myself (and introduce bugs
    while doing so :))?

    Cheers,
    Daniel

    --
    Psss, psss, put it down! - http://www.cafepress.com/putitdown
    Daniel Fetchinson, Dec 9, 2008
    #1
    1. Advertising

  2. Daniel Fetchinson

    Peter Otten Guest

    Daniel Fetchinson wrote:

    > I came across a javascript library that returns all sorts of html
    > codes in the cookies it sets and I need my web framework (written in
    > python :)) to decode them. I'm aware of htmlentitydefs but
    > htmlentitydefs.entitydefs.keys( ) are of the form '&#xxx' but this
    > javascript library uses stuff like '%3A' for the ':' for example. The
    > conversion is here:
    >
    > http://www.ascii.cl/htmlcodes.htm
    >
    > Is there a python package/module/whatever that does the conversion for
    > me or do I have to write a little wrapper myself (and introduce bugs
    > while doing so :))?


    >>> import urllib
    >>> urllib.quote("Löblich ähnlich üblich")

    'L%C3%B6blich%20%C3%A4hnlich%20%C3%BCblich'
    >>> urllib.unquote(_)

    'L\xc3\xb6blich \xc3\xa4hnlich \xc3\xbcblich'
    >>> print _

    Löblich ähnlich üblich

    If you care about the encoding you have to encode/decode explicitly:

    >>> urllib.quote(u"Löblich ähnlich üblich".encode("latin1"))

    'L%F6blich%20%E4hnlich%20%FCblich'
    >>> urllib.unquote(_).decode("latin1")

    u'L\xf6blich \xe4hnlich \xfcblich'

    Peter
    Peter Otten, Dec 9, 2008
    #2
    1. Advertising

  3. >> I came across a javascript library that returns all sorts of html
    >> codes in the cookies it sets and I need my web framework (written in
    >> python :)) to decode them. I'm aware of htmlentitydefs but
    >> htmlentitydefs.entitydefs.keys( ) are of the form '&#xxx' but this
    >> javascript library uses stuff like '%3A' for the ':' for example. The
    >> conversion is here:
    >>
    >> http://www.ascii.cl/htmlcodes.htm
    >>
    >> Is there a python package/module/whatever that does the conversion for
    >> me or do I have to write a little wrapper myself (and introduce bugs
    >> while doing so :))?

    >
    >>>> import urllib
    >>>> urllib.quote("Löblich ähnlich üblich")

    > 'L%C3%B6blich%20%C3%A4hnlich%20%C3%BCblich'
    >>>> urllib.unquote(_)

    > 'L\xc3\xb6blich \xc3\xa4hnlich \xc3\xbcblich'
    >>>> print _

    > Löblich ähnlich üblich
    >
    > If you care about the encoding you have to encode/decode explicitly:
    >
    >>>> urllib.quote(u"Löblich ähnlich üblich".encode("latin1"))

    > 'L%F6blich%20%E4hnlich%20%FCblich'
    >>>> urllib.unquote(_).decode("latin1")

    > u'L\xf6blich \xe4hnlich \xfcblich'


    Thanks a lot guys!

    Cheers,
    Daniel


    --
    Psss, psss, put it down! - http://www.cafepress.com/putitdown
    Daniel Fetchinson, Dec 9, 2008
    #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. Alexandre Soares

    Converting accent characters to html codes

    Alexandre Soares, Sep 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    668
  2. Greg  --
    Replies:
    4
    Views:
    2,128
  3. news frontiernet.net
    Replies:
    10
    Views:
    887
    Karl Groves
    Aug 13, 2004
  4. Replies:
    2
    Views:
    2,792
    Malcolm
    Aug 20, 2005
  5. Allen
    Replies:
    1
    Views:
    624
    Mark Rae [MVP]
    Dec 3, 2007
Loading...

Share This Page