Python's urllib.quote and urllib.unquote do not handle Unicode correctly
urllib
does not handle Unicode at all. URLs don't contain non-ASCII characters, by definition. When you're dealing with urllib
you should use only byte strings. If you want those to represent Unicode characters you will have to encode and decode them manually.
IRIs can contain non-ASCII characters, encoding them as UTF-8 sequences, but Python doesn't, at this point, have an irilib
.
Encoding the value to UTF8 also does not work:
In [6]: print urllib.unquote(urllib.quote(u'Cata?o'.encode('utf8')))
Cata?±o
Ah, well now you're typing Unicode into a console, and doing print
-Unicode to the console. This is generally unreliable, especially in Windows and in your case with the IPython console.
Type it out the long way with backslash sequences and you can more easily see that the urllib
bit does actually work:
>>> u'Catau00F1o'.encode('utf-8')
'CataxC3xB1o'
>>> urllib.quote(_)
'Cata%C3%B1o'
>>> urllib.unquote(_)
'CataxC3xB1o'
>>> _.decode('utf-8')
u'CataxF1o'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…