BoobaGump
BoobaGump

Reputation: 535

Reorganize a string. Split digits from text

While scraping a website i received from the get_text()in Beautifulsoup :

protein  30 %, crude fibres  2.6 %, fat content  15 %, crude ash  7.7 %, Vitamin E  180 mg/kg, omega-3 fatty acids  1.5 %, omega-6 fatty acids  1.4 %

The aim is to get a csv looking like :

protein ; 30%
crude fibres ; 2,6%
fat content ; 15 %
...
omega-6 fatty acids ; 1,4%

But i need to keep my logics of scrapping. That's why i need to create pair_list=[name,quantity] like pair_list=[protein,30%]

How can i create such a pair ?

Upvotes: 1

Views: 108

Answers (2)

poke
poke

Reputation: 388023

Assuming you always have that two space separator:

>>> s = 'protein  30 %, crude fibres  2.6 %, fat content  15 %, crude ash  7.7 %, Vitamin E  180 mg/kg, omega-3 fatty acids  1.5 %, omega-6 fatty acids  1.4 %'
>>> [x.strip().split('  ') for x in s.split(',')]
[['protein', '30 %'], ['crude fibres', '2.6 %'], ['fat content', '15 %'], ['crude ash', '7.7 %'], ['Vitamin E', '180 mg/kg'], ['omega-3 fatty acids', '1.5 %'], ['omega-6 fatty acids', '1.4 %']]

>>> for x in _:
        print(x)

['protein', '30 %']
['crude fibres', '2.6 %']
['fat content', '15 %']
['crude ash', '7.7 %']
['Vitamin E', '180 mg/kg']
['omega-3 fatty acids', '1.5 %']
['omega-6 fatty acids', '1.4 %']

Upvotes: 1

Kasravnd
Kasravnd

Reputation: 107347

You can use re.split within a list comprehension :

>>> [re.split(r' (?=\d+)',i) for i in s.split(',')]
[['protein ', '30 %'], [' crude fibres ', '2.6 %'], [' fat content ', '15 %'], [' crude ash ', '7.7 %'], [' Vitamin E ', '180 mg/kg'], [' omega-3 fatty acids ', '1.5 %'], [' omega-6 fatty acids ', '1.4 %']]

The regex r' (?=\d+)' use a positive look-ahead which makes the re.split split your regex based on a space that followed by a number.

And then you can write the result to a csv file :

import csv
with open('my_file.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',')
    spamwriter.writerows(list_result)

Upvotes: 1

Related Questions