gpt4 book ai didi

python - Unicode 警告 : Unicode equal comparison failed to convert both arguments to Unicode

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:11 24 4
gpt4 key购买 nike

我知道很多人以前遇到过这个错误,但我找不到解决问题的办法。

我有一个要规范化的 URL:

url = u"http://www.dgzfp.de/Dienste/Fachbeitr%C3%A4ge.aspx?EntryId=267&Page=5"
scheme, host_port, path, query, fragment = urlsplit(url)
path = urllib.unquote(path)
path = urllib.quote(path,safe="%/")

这给出了一条错误信息:

/usr/lib64/python2.6/urllib.py:1236: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
res = map(safe_map.__getitem__, s)
Traceback (most recent call last):
File "url_normalization.py", line 246, in <module>
logging.info(get_canonical_url(url))
File "url_normalization.py", line 102, in get_canonical_url
path = urllib.quote(path,safe="%/")
File "/usr/lib64/python2.6/urllib.py", line 1236, in quote
res = map(safe_map.__getitem__, s)
KeyError: u'\xc3'

我试图从 URL 字符串中删除 unicode 指示符“u”,但我没有收到错误消息。但是我怎样才能自动摆脱 unicode 因为我直接从数据库中读取它。

最佳答案

urllib.quote() 没有正确解析 Unicode。要解决这个问题,您可以在读取 url 时调用 .encode() 方法(或从数据库中读取的变量)。所以运行 url = url.encode('utf-8')。有了这个,您将获得:

import urllib
import urlparse
from urlparse import urlsplit

url = u"http://www.dgzfp.de/Dienste/Fachbeitr%C3%A4ge.aspx?EntryId=267&Page=5"
url = url.encode('utf-8')
scheme, host_port, path, query, fragment = urlsplit(url)
path = urllib.unquote(path)
path = urllib.quote(path,safe="%/")

path 变量的输出将是:

>>> path
'/Dienste/Fachbeitr%C3%A4ge.aspx'

这个有用吗?

关于python - Unicode 警告 : Unicode equal comparison failed to convert both arguments to Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28206723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com