Search code examples
stringpython-3.xdigits

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 ?


Solution

  • 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 %']