Home > Blockchain >  How to split text value on comma?
How to split text value on comma?

Time:01-27

How to split a text value on comma, to get two datapoints from it?


My code:

details = response.xpath('//div[@]')
for detail in details:
    body = detail.xpath('.//ul/li[1]/span[2]/span/text()').get()
    item['body'] = released
    yield item

My output:

293g (Wi-Fi) / 297g (Wi-Fi Cellular), 6.3mm thickness


Desired output (Column A) | Weight

293g (Wi-Fi) / 297g (Wi-Fi Cellular)

Desired output (Column B) | Dimensions

6.3mm


CodePudding user response:

.split() takes an argument, so you can do this .split(", ") and unpack to weight and thickness.

Try this:

details = response.xpath('//div[@]')
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
  •  Tags:  
  • Related