Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a comma separated string to separate strings within a list in Python [duplicate]

I have a input string like this:

pentavac-xim, revaxis, tetravac-xim
imovax polio, pediacel
act-hib, rage diploide
imogam rage pa, imovax polio
dt vax, tetavax

I want the Output string to displayed as:

['pentavac-xim', 'revaxis', 'tetravac-xim']
['imovax polio', 'pediacel']
['act-hib', 'rage diploide']
['imogam rage pa', 'imovax polio']
['dt vax', 'tetavax']

The problem is when I use split on the input string, for a product which should be treated as a single string, is converted into 2 separate strings, example:

imogam rage pa = ['imogam', 'rage', 'pa'] which is incorrect . It should be : ['imogam rage pa']. How to solve this

like image 284
Swordsman Avatar asked Dec 04 '22 18:12

Swordsman


1 Answers

You could try something like:

input_string = 'pentavac-xim, revaxis, tetravac-xim'
output = input_string.split(', ')
print(output)

Outputs: ['pentavac-xim', ' revaxis', ' tetravac-xim']

like image 173
Reedinationer Avatar answered Dec 06 '22 11:12

Reedinationer