UTF-8 percentage encoding and python

后端 未结 2 2054
醉梦人生
醉梦人生 2021-01-02 22:14

I\'m trying to get python to give me percent encoded strings. The API I\'m interacting with (which I think is using percent encoded UTF-8), gives %c3%ae for î. However, pyth

2条回答
  •  忘掉有多难
    2021-01-02 22:59

    Your file has to encode your string as utf-8 before quoting it, and the string should be unicode. Also you have to specify the appropriate file encoding for your source file in the coding section:

    # -*- coding: utf-8 -*-
    
    import urllib
    
    s = u'î'
    print urllib.quote(s.encode('utf-8'))
    

    Gives me the output:

    %C3%AE
    

提交回复
热议问题