How to escape a pipe ( | ) symbol for url_encode in python

前端 未结 2 2079
庸人自扰
庸人自扰 2021-02-19 14:05

I am facing a problem with urllib.url_encode in python. Bets explained with some code:

>>> from urllib import urlencode
>>> params = {\'p\' :          


        
2条回答
  •  -上瘾入骨i
    2021-02-19 14:38

    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'  
    

提交回复
热议问题