逃离html%20

Aus*_*inM 1 python string escaping format-string

我正在使用Python开发一些东西,我需要从%20(URL中的空格)中逃脱.例如:

"%20space%20in%20%d--" % ordnum
Run Code Online (Sandbox Code Playgroud)

所以我需要使用%20作为URL,然后使用%d作为数字.但我得到这个错误:

TypeError: not enough arguments for format string
Run Code Online (Sandbox Code Playgroud)

我知道问题是什么我只是不知道如何从%20中逃脱并修复它.

Sve*_*ach 6

一种方法是将%角色加倍:

"%%20space%%20in%%20%d--" % ordnum
Run Code Online (Sandbox Code Playgroud)

但可能更好的方法是使用urllib.quote_plus():

urllib.quote_plus(" space in %d--" % ordnum)
Run Code Online (Sandbox Code Playgroud)