Transform URL string into normal string in Python ( to space etc)

前端 未结 3 955
半阙折子戏
半阙折子戏 2020-12-14 15:07

Is there any way in Python to transform this %CE%B1%CE%BB%20 into this αλ which is its real representation?

3条回答
  •  Happy的楠姐
    2020-12-14 15:37

    There are two encodings in play here. Your string has first been encoded as UTF-8, then each byte has been percent-encoded.

    To get the original string back you need to first unquote it, and then decode it:

    >>> import urllib
    >>> s = '%CE%B1%CE%BB%20'
    >>> result = urllib.unquote(s).decode('utf8')
    >>> print result
    αλ 
    

    Note that you need a Unicode enabled console in order to display the value (if you get an error with the print statement, try running it in IDLE).

提交回复
热议问题