Python:导入urllib.quote [英] Python: Importing urllib.quote
问题描述
我想使用 urllib.quote()
。但是python(python3)没有找到模块。
假设,我有这行代码:
I would like to use urllib.quote()
. But python (python3) is not finding the module.
Suppose, I have this line of code:
print(urllib.quote("châteu", safe=''))
如何导入urllib.quote?
How do I import urllib.quote?
import urllib
或
import urllib.quote
both give
import urllib
or
import urllib.quote
both give
AttributeError: 'module' object has no attribute 'quote'
让我感到困惑的是 urllib.request
可通过 import urllib.request
访问
What confuses me is that urllib.request
is accessible via import urllib.request
推荐答案
在Python 3.x中,您需要导入 urllib.parse.quote
:
In Python 3.x, you need to import urllib.parse.quote
:
>>> import urllib.parse
>>> urllib.parse.quote("châteu", safe='')
'ch%C3%A2teu'
According to Python 2.x urllib
module documentation:
注意
urllib
模块已拆分为多个部分,并在Python 3中重命名为
urllib.request
, urllib.parse
, urllib.error
。
The urllib
module has been split into parts and renamed in Python 3 to
urllib.request
, urllib.parse
, and urllib.error
.
这篇关于Python:导入urllib.quote的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!