python exceptions.UnicodeDecodeError: 'ascii' 编解码器无法解码字节 0xa7

编程入门 行业动态 更新时间:2024-10-23 23:27:13
本文介绍了python exceptions.UnicodeDecodeError: 'ascii' 编解码器无法解码字节 0xa7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am using scrapy with python and I have this code in a python item pipline

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'dubai.dubizzle/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

as you see the item['link'] is unicode

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

when I change the item['link'] to any other attribute like item['date'] the code works perfectly

推荐答案

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

Encode the unicode string into byte string with .encode('utf-8') and it should work:

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

更多推荐

python exceptions.UnicodeDecodeError: 'ascii' 编解码器无法解码字节 0xa7

本文发布于:2023-10-24 16:58:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字节   编解码器   UnicodeDecodeError   exceptions   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!