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

前端 未结 2 2065
庸人自扰
庸人自扰 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条回答
  • 2021-02-19 14:23

    It is simpler in Python 3:

    urllib.parse.urlencode(params, safe='|')
    
    0 讨论(0)
  • 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'  
    
    0 讨论(0)
提交回复
热议问题