I am facing a problem with urllib.url_encode in python. Bets explained with some code:
>>> from urllib import urlencode
>>> params = {'p' : '1 2 3 4 5&6', 'l' : 'ab|cd|ef'}
>>> urlencode(params)
'p=1+2+3+4+5%266&l=ab%7Ccd%7Cef'
I want to keep the pipes ('|') in to l parameter. can you please tell me how?
The result should be
'p=1+2+3+4+5%266&l=ab|cd|ef'
PS: I do not want to put together the URL manually, but use urlencode for that.
Thanks -Pat
Python Regex Escape Pipe You can get rid of the special meaning of the pipe symbol by using the backslash prefix: \| . This way, you can match the parentheses characters in a given string. Here's an example: What is this?
s = urllib2. quote(s) # URL encode. # Now "s" is encoded the way you need it. It works!
parse. urlencode() method can be used for generating the query string of a URL or data for a POST request.
Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string[...]
The urlencode() method is acting as expected. If you want to prevent the encoding then you can first encode the entire object and then replace the encoded characters with pipes.
>>> u = urlencode(params)
>>> u.replace('%7C', '|')
'p=1+2+3+4+5%266&l=ab|cd|ef'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With