2011-06-21 36 views
17

我想編碼非ASCII字符,所以我可以把它們放在一個URL中,並在urlopen中使用它們。問題是,我要像JavaScript編碼(即例如編碼ó%C3%B3):Python編碼與urllib.quote字符

encodeURIComponent(ó) 
'%C3%B3' 
在蟒蛇回報

urllib.quoteó%F3

urllib.quote(ó) 
'%F3' 

我想知道如何實現像Python的encodeURIComponent這樣的編碼,以及如果我可以編碼非中文字符的非ISO 8859-1字符。謝謝!

+0

相關:http://stackoverflow.com/questions/6338469/how-to-url-safe-encode-a-string -with-python-and-urllib-quote-is-wrong – geoffspear

回答

27

你想確保你使用的是unicode。

實施例:

import urllib 

s = u"ó" 
print urllib.quote(s.encode("utf-8")) 

輸出:

%C3%B3

+0

謝謝......它已解決 – Saulpila