【发布时间】:2018-12-11 07:02:21
【问题描述】:
只是一个 scrapy.org 的新用户和 Python 的新手。我在包含制表符空格和换行符的 brand 和 title 属性(JAVA OOP Term)中有这个值。我们如何修剪它以使以下 2 个对象属性具有此纯字符串值
item['brand'] = "KORAL ACTIVEWEAR"
item['title'] = "Boom Leggings"
下面是数据结构
{'store_id': 870, 'sale_price_low': [], 'brand': [u'\n KORAL ACTIVEWEAR\n '], 'currency': 'AUD', 'retail_price': [u'$140.00'], 'category': [u'Activewear'], 'title': [u'\n Boom Leggings\n '], 'url': [u'/boom-leggings-koral-activewear/vp/v=1/1524019474.htm?folderID=13331&fm=other-shopbysize-viewall&os=false&colorId=68136'], 'sale_price_high': [], 'image_url': [u' https://images-na.sample-store.com/images/G/01/samplestore/p/prod/products/kacti/kacti3025868136/kacti3025868136_q1_2-0._SH20_QL90_UY365_.jpg\n'], 'category_link': 'https://www.samplestore.com/clothing-activewear/br/v=1/13331.htm?baseIndex=500', 'store': 'SampleStore'}
我能够通过使用正则表达式搜索方法来修剪价格以仅获取数字和小数,我认为当有价格逗号分隔符时这可能是错误的。
price = re.compile('[0-9\.]+')
item['retail_price'] = filter(price.search, item['retail_price'])
【问题讨论】:
-
它看起来只有前导和尾随空格 - 像
x.strip()这样的东西应该足够好。为什么是正则表达式?
标签: python python-2.7 scrapy scrapy-pipeline