Python 常用库小结

1. urllib.parse

urlencode (对dict操作)

>>> from urllib import parse
>>> query = {
  'name': 'walker',
![Uploading Paste_Image_727749.png . . .]
  'age': 99,
  }
>>> parse.urlencode(query)
'name=walker&age=99'

quote/quote_plus (对str操作)

>>> from urllib import parse
>>> parse.quote('a&b/c')  #未编码斜线
'a%26b/c'
>>> parse.quote_plus('a&b/c')  #编码了斜线
'a%26b%2Fc'

unquote/unquote_plus (对str操作)

from urllib import parse
>>> parse.unquote('1+2')  #不解码加号
'1+2'
>>> parse.unquote('1+2')  #把加号解码为空格
'1 2'

你可能感兴趣的:(Python 常用库小结)