如何在逗号上拆分文本值?
How to split text value on comma?
如何用逗号拆分文本值,从中获取两个数据点?
我的代码:
details = response.xpath('//div[@class="article-info"]')
for detail in details:
body = detail.xpath('.//ul/li[1]/span[2]/span/text()').get()
item['body'] = released
yield item
我的输出:
293g (Wi-Fi) / 297g (Wi-Fi + Cellular), 6.3mm thickness
期望的输出(A 列)|体重
293g (Wi-Fi) / 297g (Wi-Fi + Cellular)
期望的输出(B 列)|维度
6.3mm
.split()
需要一个参数,所以你可以这样做 .split(", ")
并解压到 weight
和 thickness
.
试试这个:
details = response.xpath('//div[@class="article-info"]')
for detail in details:
weight, thickness = detail.xpath('.//ul/li[1]/span[2]/span/text()').get().split(", ")
item['weight'] = weight
item['thickness'] = thickness.split()[0] # gets you 6.3mm only
yield item
如何用逗号拆分文本值,从中获取两个数据点?
我的代码:
details = response.xpath('//div[@class="article-info"]')
for detail in details:
body = detail.xpath('.//ul/li[1]/span[2]/span/text()').get()
item['body'] = released
yield item
我的输出:
293g (Wi-Fi) / 297g (Wi-Fi + Cellular), 6.3mm thickness
期望的输出(A 列)|体重
293g (Wi-Fi) / 297g (Wi-Fi + Cellular)
期望的输出(B 列)|维度
6.3mm
.split()
需要一个参数,所以你可以这样做 .split(", ")
并解压到 weight
和 thickness
.
试试这个:
details = response.xpath('//div[@class="article-info"]')
for detail in details:
weight, thickness = detail.xpath('.//ul/li[1]/span[2]/span/text()').get().split(", ")
item['weight'] = weight
item['thickness'] = thickness.split()[0] # gets you 6.3mm only
yield item