python - Scrapy how to handle pound sign? -
python - Scrapy how to handle pound sign? -
i'm new python , scrapy , i'm trying test if string (extracted using xpath selector) contains pound symbol (british currency - £).
at top of source file have specified encoding:
# -*- coding: latin-1 -*-
and i'm performing test:
if '£' in price: ...
however i'm getting error exceptions.unicodedecodeerror: 'ascii' codec can't decode byte 0xc2.
if alter test to
price = price.encode('utf-8') if '£' in price: ...
it works. can explain why price.encode() phone call necessary, understood scrapy returns unicode strings anyway. many thanks
# these have different types: if some_string in some_unicode_object
doing equivalent writing:
# convert first argument can `in` if some_string.decode('ascii') in some_unicode_object
so in example:
if '£' in price: # ^string ^unicode
you're calling '£'.encode('ascii')
, fails because isn't ascii bytestring.
a improve way of writing be:
if u'£' in price:
alternatively, may want write from __future__ import unicode_literals
.
python scrapy
Comments
Post a Comment