Simple ascii url encoding with python

后端 未结 6 1303
不思量自难忘°
不思量自难忘° 2021-01-16 12:23

look at that:

import urllib
print urllib.urlencode(dict(bla=\'Ã\'))

the output is

bla=%C3%BC

what I want

6条回答
  •  深忆病人
    2021-01-16 12:49

    I want the output in ascii instead of utf-8

    That's not ASCII, which has no characters mapped above 0x80. You're talking about ISO-8859-1, or possibly code page 1252 (the Windows encoding based on it).

    'Ã'.decode('iso-8859-1')
    

    Well that depends on what encoding you've used to save the character à in the source, doesn't it? It sounds like your text editor has saved it as UTF-8. (That's a good thing, because locale-specific encodings like ISO-8859-1 need to go away ASAP.)

    Tell Python that the source file you've saved is in UTF-8 as per PEP 263:

    # coding=utf-8
    
    urllib.quote(u'Ã'.encode('iso-8859-1'))    # -> %C3
    

    Or, if you don't want that hassle, use a backslash escape:

    urllib.quote(u'\u00C3'.encode('iso-8859-1'))    # -> %C3
    

    Although, either way, a modern webapp should be using UTF-8 for its input rather than ISO-8859-1/cp1252.

提交回复
热议问题