class - url encode 與 decode

最後更新: 2014-11-28

 

目錄

  • URL Encoded value:
  • encode 與 decode
  • Example

 


URL Encoded value

 

  • %20    space
  • %2F     /   
  • %28     (    
  • %29     )
  • %3F     ?
  • %3D    =
  • ...

 


encode 與 decode

 

- urlencode
- quote
- unquote

urlencode:

# encoding: utf-8
from urllib import urlencode
data = {
    'a': 'test',
    'b': '測試'
}
print urlencode(data)

output:

a=test&b=%E6%B8%AC%E8%A9%A6

quote:

# encoding: utf-8
from urllib import quote
print quote('測試')

output:

%E6%B8%AC%E8%A9%A6

unquote:

# encoding: utf-8
from urllib import unquote
print unquote('%E6%B8%AC%E8%A9%A6')

 


Example

 

>>> import urllib

>>> urllib.quote('中文測試')
'%E4%B8%AD%E6%96%87%E6%B8%AC%E8%A9%A6'

>>> urllib.quote(u'中文測試'.encode('utf-8'))
'%E4%B8%AD%E6%96%87%E6%B8%AC%E8%A9%A6'

# 當有 "u" 時, 那就要用 "encode"

str = "測試"
str = unicode(str, 'big5')

 

 

 

 

 

Creative Commons license icon Creative Commons license icon