gpt4 book ai didi

python异常.UnicodeDecodeError : 'ascii' codec can't decode byte 0xa7 in

转载 作者:太空狗 更新时间:2023-10-29 21:37:18 25 4
gpt4 key购买 nike

我在 python 中使用 scrapy,我在 python 项目管道中有这段代码

def process_item(self, item, spider):
import pdb; pdb.set_trace()
ID = str(uuid.uuid5(uuid.NAMESPACE_DNS, item['link']))

我遇到了这个错误:

        Traceback (most recent call last):
File "C:\Python27\lib\site-packages\scrapy-0.20.2-py2.7.egg\scrapy\mid
dleware.py", line 62, in _process_chain
return process_chain(self.methods[methodname], obj, *args)
File "C:\Python27\lib\site-packages\scrapy-0.20.2-py2.7.egg\scrapy\uti
ls\defer.py", line 65, in process_chain
d.callback(input)
File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 3
82, in callback
self._startRunCallbacks(result)
File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 4
90, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 5
77, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "General_Spider_code_version_2\pipelines.py", line 7, in process_
item
ID = str(uuid.uuid5(uuid.NAMESPACE_DNS, item['link']))
File "C:\Python27\lib\uuid.py", line 549, in uuid5
hash = sha1(namespace.bytes + name).digest()
exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0xa7 in p
osition 1: ordinal not in range(128)

我尝试调试 item['link']

这是结果

-> ID = str(uuid.uuid5(uuid.NAMESPACE_DNS, item['link']))
(Pdb) item['link']
u'http://dubai.dubizzle.com/property-for-rent/residential/apartmentflat/2014/4/6
/palm-jumeirah-abu-keibal-3-br-maid-partial-2/?back=ZHViYWkuZHViaXp6bGUuY29tL3By
b3BlcnR5LWZvci1yZW50L3Jlc2lkZW50aWFsL2FwYXJ0bWVudGZsYXQv&pos=1'
(Pdb)

如您所见,item['link'] 是 unicode

编辑1

当我将 item['link'] 更改为任何其他属性(如 item['date'] 时,代码可以完美运行

最佳答案

使用 .encode('utf-8') 将 unicode 字符串编码为字节字符串,它应该可以工作:

str(uuid.uuid5(uuid.NAMESPACE_DNS, item['link'].encode('utf-8')))

关于python异常.UnicodeDecodeError : 'ascii' codec can't decode byte 0xa7 in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23944571/

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