您的位置:首页 > 编程语言 > Python开发

python3:AttributeError: module 'urllib' has no attribute 'quote'

2018-02-11 13:28 2231 查看
python2 与python3版本不兼容的问题真是让人诟病,现在又报错了,我的错误信息为:

Traceback (most recent call last):
File "/Users/eric/Documents/pythonFiles/aliyunxiaomi/chatwithxiaomi/chatwithali.py", line 22, in <module>
canstring += '&' + percentEncode(k) + '=' + percentEncode(v)
File "/Users/eric/Documents/pythonFiles/aliyunxiaomi/chatwithxiaomi/chatwithali.py", line 14, in percentEncode
res = urllib.quote(str.decode(sys.stdin.encoding).encode('utf8'), '')
AttributeError: module 'urllib' has no attribute 'quote'

然后,查阅相关资料,我的修改为:
import urllib.parse
def percentEncode(str):
res = urllib.parse.quote(str, '')
res = res.replace('+', '%20')
res = res.replace('*', '%2A')
res = res.replace('%7E', '~')
return res

这样就不包错误了。

参考文献

[1].Python: Importing urllib.quote.https://stackoverflow.com/questions/31827012/python-importing-urllib-quote
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐