Re: Problem: neither urllib2.quote nor urllib.quote encode theunicode strings arguments

Discussion in 'Python' started by Jerry Hill, Oct 4, 2008.

  1. Jerry Hill

    Jerry Hill Guest

    On Fri, Oct 3, 2008 at 5:38 PM, Valery Khamenya <> wrote:
    > Hi all
    > things like urllib.quote(u"пиво Müller ") fail with error message:
    > <type 'exceptions.KeyError'>: u'\u043f'
    > Similarly with urllib2.
    > Anyone got a hint?? I need it to form the URI containing non-ascii chars.


    Do you know what, exactly, you'd like the result to be? The encoding
    of unicode characters into URIs is not well defined. My understanding
    is that the most common case is to percent-encode UTF-8, like this:

    >>> u = u"Müller"
    >>> import urllib
    >>> urllib.quote(u.encode('utf8'))

    'M%C3%BCller'

    If you need to, you can encode your unicode string differently, like this:

    >>> urllib.quote(u.encode('latin-1'))

    'M%FCller'

    --
    Jerry
    Jerry Hill, Oct 4, 2008
    #1
    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. Luigi Donatello Asero

    A text which neither gets very narrow nor disappears

    Luigi Donatello Asero, Oct 3, 2005, in forum: HTML
    Replies:
    21
    Views:
    869
    Jonathan N. Little
    Jan 6, 2006
  2. -berlin.de
    Replies:
    7
    Views:
    1,064
    Richard Tobin
    Feb 26, 2007
  3. pabbu
    Replies:
    8
    Views:
    719
    Marc Boyer
    Nov 7, 2005
  4. Valery Khamenya
    Replies:
    3
    Views:
    708
  5. steveski
    Replies:
    3
    Views:
    776
    Ken Cox [Microsoft MVP]
    Jan 26, 2005
Loading...

Share This Page