Sunday, May 1, 2011

Unescape Python Strings From HTTP

I've got a string from an HTTP header, but it's been escaped.. what function can I use to unescape it?

myemail%40gmail.com -> myemail@gmail.com

Would urllib.unquote() be the way to go?

From stackoverflow
  • I am pretty sure that urllib's unquote is the common way of doing this.

    >>> import urllib
    >>> urllib.unquote("myemail%40gmail.com")
    'myemail@gmail.com'
    

    There's also unquote_plus:

    Like unquote(), but also replaces plus signs by spaces, as required for unquoting HTML form values.

    Ian : K, just wanted to make sure.. I hate using a function that appears to do the job, but ends up only working with a few examples that I did and breaking with real world vars. heh. Then it becomes impossible to track down the problem.. :P
  • Yes, it appears that urllib.unquote() accomplishes that task. (I tested it against your example on codepad.)

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.