python exceptions.UnicodeDecodeError: ascii codec cant decode byte 0xa7 in

2024/10/11 18:24:43

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']))

I got this error :

        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_chainreturn 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_chaind.callback(input)File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 3
82, in callbackself._startRunCallbacks(result)File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 4
90, in _startRunCallbacksself._runCallbacks()--- <exception caught here> ---File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 5
77, in _runCallbackscurrent.result = callback(current.result, *args, **kw)File "General_Spider_code_version_2\pipelines.py", line 7, in process_
itemID = str(uuid.uuid5(uuid.NAMESPACE_DNS, item['link']))File "C:\Python27\lib\uuid.py", line 549, in uuid5hash = sha1(namespace.bytes + name).digest()exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0xa7 in p
osition 1: ordinal not in range(128)

I tried to debug the item['link']

and this is the result

-> 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)

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

Edit1

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

Answer

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')))
https://en.xdnf.cn/q/69742.html

Related Q&A

Trace Bug which happends only sometimes in CI

I have a strange bug in python code which only happens sometimes in CI.We cant reproduce it.Where is the test code:response=self.admin_client.post(url, post) self.assertEqual(200, response.status_code,…

Limit neural network output to subset of trained classes

Is it possible to pass a vector to a trained neural network so it only chooses from a subset of the classes it was trained to recognize. For example, I have a network trained to recognize numbers and l…

Unable to fully remove border of PyQt QGraphicsView

I have tried calling self.setStyleSheet("background: transparent; border: transparent;") on a QGraphicsView, but it still leaves a 1 pixel border on the top edge. I have also tried replacing …

SqlAlchemy non persistent column

I am trying to define a model using sqlalchemy such that one of the columns i only need in memory for processing, i do not have a corresponding database column for that. I need it such that when i save…

Creating command line alias with python

I want to create command line aliases in one of my python scripts. Ive tried os.system(), subprocess.call() (with and without shell=True), and subprocess.Popen() but I had no luck with any of these me…

Remove unwanted lines in captcha text - opencv - python

I trying to get text from captcha image using opencv. Problem is text are masked with noise and it is complex to process with those horizontal line/noise.Original imageMy processed image :not sure how …

Double Summation in Python

I am trying to write a code to conduct a double summation (see pic) in which; M is the subjects, N is the Trials, Yijt is the measured wave form data (3d array)so far I have; Given Y is the data arra…

psycopg, double and single quotes insert

I ran into problems, while trying to insert to database:ur_psql.execute("""insert into smth(data, filedate, filedby)"""""" values(%s, NOW(), %s)""…

How to debug Python 2.7 code with VS Code?

For work I have to use Python 2.7. But when I use the "debug my python file" function in VS Code, I get an error. Even with a simple program, like : print()

Python Sequence of Numbers

Ive decided not to waste my summer and start learning python. I figured Id start learning looping techniques so I wanted to start with a basic list of numbers, aka, write a for loop that will generate…