在带有“safe”参数的utf-8字符串上使用python的urllib.quote撸plus

2024-04-20 07:49:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python代码中有一个unicode字符串:

name = u'Mayte_Martín'

我想使用SPARQL查询,这意味着我应该使用“UTF-8”来编码字符串,并使用URLLIB QuoTeTyPub或Realest.Queon。但是,这两个引号函数的行为都很奇怪,这一点在使用和不使用“safe”参数时都可以看到。

from urllib import quote_plus

没有“安全”参数:

quote_plus(name.encode('utf-8'))
Output: 'Mayte_Mart%C3%ADn'

带“安全”参数:

quote_plus(name.encode('utf-8'), safe=':/')
Output: 
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-164-556248391ee1> in <module>()
----> 1 quote_plus(v, safe=':/')

/usr/lib/python2.7/urllib.pyc in quote_plus(s, safe)
   1273         s = quote(s, safe + ' ')
   1274         return s.replace(' ', '+')
-> 1275     return quote(s, safe)
   1276 
   1277 def urlencode(query, doseq=0):

/usr/lib/python2.7/urllib.pyc in quote(s, safe)
   1264         safe = always_safe + safe
   1265         _safe_quoters[cachekey] = (quoter, safe)
-> 1266     if not s.rstrip(safe):
   1267         return s
   1268     return ''.join(map(quoter, s))

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)

问题似乎出在rstrip函数上。我试着做些改变然后打电话给。。。

quote_plus(name.encode('utf-8'), safe=u':/'.encode('utf-8'))

但这并没有解决问题。有什么问题吗?


Tags: 函数字符串nameinoutput参数returnplus
3条回答
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import urllib
name = u'Mayte_Martín'
print urllib.quote_plus(name.encode('utf-8'), safe=':/')

对我来说没问题(Py 2.7.9,Debian)

(我不知道答案,但我不能就名誉发表评论)

我在回答我自己的问题,这样可以帮助其他面临同样问题的人。

当您在执行任何其他操作之前在当前工作区中进行以下导入时,会出现此特定问题。

from __future__ import unicode_literals

这与下面的代码序列不兼容。

from urllib import quote_plus

name = u'Mayte_Martín'
quote_plus(name.encode('utf-8'), safe=':/')

不导入unicode字符的相同代码可以正常工作。

根据this bug,以下是解决方法:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from urllib import quote_plus
name = u'Mayte_Martín'
quote_plus(name.encode('utf-8'), safe=':/'.encode('utf-8'))

必须在quotequote_plus方法中同时使用encode两个参数才能utf-8

相关问题 更多 >