Urllib 引用问题:处理 â latin-1 数据库中的字符

发布于 2024-09-09 11:23:52 字数 483 浏览 1 评论 0原文

我需要将 â 字符转换为可以传递到 URL 的格式。我以 json 列表的形式获取一些名称,然后将它们传递到其他地方。

result = json.load(urllib2.urlopen(LIST_URL), encoding='latin-1')
for item in result:
    name = item["name"]
    print name
    print urllib2.quote(name.lower())

当名称为 Siân 时,这会产生 urllib 错误:

Siân
Line 24 - print urllib2.quote(mp_name.lower())
/usr/lib/python2.6/urllib.py -- quote((s=u'si\xe2n', safe='/'))
KeyError(u'\xe2')

请问有人可以建议吗?

I need to get an â character into a format that can be passed to a URL. I'm obtaining some names as a json list, and then passing them elsewhere.

result = json.load(urllib2.urlopen(LIST_URL), encoding='latin-1')
for item in result:
    name = item["name"]
    print name
    print urllib2.quote(name.lower())

This produces a urllib error when the name is Siân:

Siân
Line 24 - print urllib2.quote(mp_name.lower())
/usr/lib/python2.6/urllib.py -- quote((s=u'si\xe2n', safe='/'))
KeyError(u'\xe2')

Please could anyone advise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2024-09-16 11:23:52

quote() 函数需要 str 参数,而不是 unicode。使用 urllib2.quote(name.lower().encode('latin1')) (假设您的网站接受 latin1 编码)。

quote() function requires str argument, not unicode. Use urllib2.quote(name.lower().encode('latin1')) (assuming your site accepts latin1 encoding).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文