[Python] Split a string in python on a floating point number, if no floating point number...

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a list of strings and I want to split each string on a floating point number. If there is no floating point number in the string, I want to split it on a number. It should only split once and return everything before and after it separated by commas.

    Input string:

    ['Naproxen 500 Active ingredient Ph Eur',
    'Croscarmellose sodium 22.0 mg Disintegrant Ph Eur',
    'Povidone K90 11.0 Binder 56 Ph Eur',
    'Water, purifieda',
    'Silica, colloidal anhydrous 2.62 Glidant Ph Eur',
    'Magnesium stearate 1.38 Lubricant Ph Eur']


    Expected output:

    ['Naproxen', '500', 'Active ingredient Ph Eur',
    'Croscarmellose sodium', '22.0 mg', 'Disintegrant Ph Eur',
    'Povidone K90', '11.0', 'Binder Ph Eur',
    'Water, purified',
    'Silica, colloidal anhydrous', '2.62', 'Glidant Ph Eur',
    'Magnesium stearate', '1.38', 'Lubricant Ph Eur']

    Continue reading...

Compartilhe esta Página