如何编码字符串以便我可以在url params中传递编码字符串?
答案 0 :(得分:4)
使用urllib.quote_plus函数。这是一个例子:
#!/usr/bin/python
print "Content-Type: text/html;charset=utf-8";
print
import urllib
str = "Hello World?"
str = urllib.quote_plus(str)
print str
输出: 你好+世界%3F
答案 1 :(得分:1)
>>> import urllib
>>> f = { 'eventName' : 'myEvent', 'eventDescription' : "cool event"}
>>> urllib.urlencode(f)
'eventName=myEvent&eventDescription=cool+event'
答案 2 :(得分:0)
图书馆使用的是urllib
,请参阅http://docs.python.org/2/library/urllib.html
>>> import urllib
>>> f = { 'eventName' : 'myEvent', 'eventDescription' : "cool event"}
>>> urllib.urlencode(f)
'eventName=myEvent&eventDescription=cool+event'
>>> urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$')
'string_of_characters_like_these%3A%24%23%40%3D%3F%25%5EQ%5E%24'