我开始学习python3,并尝试将字符串转换为百分比编码。我使用的是urllib3。下面是写下的内容:
import urllib3
from urllib.parse import quote
quote ('/this will be the text/')
print (quote)代码的结果如下所示:
<function quote at 0x7ff77eca3d08>我真正想要的是:
this%20will%20be%20the%20text老实说,我读了urllib3的Documentation和percent encoding URL with Python的帖子,但我仍然没有运气。
我在urllib3中使用Python3。
发布于 2018-12-12 00:09:47
嗨,GuyFawkes05th,欢迎来到StackOverflow。
你正在经历的行为不是bug或问题,而是它正在做你要求它做的事情。
考虑以下代码片段在空闲时运行:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import urllib3
>>> from urllib.parse import quote
>>> quote('/this will be the text/')
'/this%20will%20be%20the%20text/'
>>> print(quote)
<function quote at 0x00000000030557B8>
>>> 您可以看到,您的文本在调用quote之后立即进行了转义,但是您的print语句没有反映这一点。这是因为您打印的是函数本身。如果你稍微修改一下你的代码,它就会像你期望的那样工作:
>>> import urllib3
>>> from urllib.parse import quote
>>> text = quote('this will be the text')
>>> print(text)
this%20will%20be%20the%20text
>>> 您可以在这里看到,我将调用quote的输出赋给了一个变量,调用文本,然后打印文本。
希望这能帮助你弄清楚一些事情!
https://stackoverflow.com/questions/53727948
复制相似问题