How do I convert a path in ASCII hex code, to its equivalent ASCII letters?

情到浓时终转凉″ 提交于 2019-12-11 04:43:25

问题


I am trying to use the following path in Python:

/home/user/Music/library/1-02%2520Maralito.mp3

The file name is: "1-02 Maralito.mp3"

So the space is being converted to the code %2520, which I do not know what represents.

I am using Rhythmbox API on Ubuntu, and I can't convert the value back in Python. Any suggestions?


回答1:


This string has been URL-encoded twice. The %25 represents a % character. The %20 resulting from decoding the %25 represents a space.

urllib.parse.unquote (just urllib.unquote in Python 2) decodes the % encoding, and you will want to decode it twice:

t = "/home/user/Music/library/1-02%2520Maralito.mp3"
from urllib.parse import unquote   # Python 3
from urllib import unquote         # Python 2
print(unquote(unquote(t)))


来源:https://stackoverflow.com/questions/46225615/how-do-i-convert-a-path-in-ascii-hex-code-to-its-equivalent-ascii-letters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!