Search code examples
pythonpython-3.xstringsplitstring-formatting

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


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


Solution

  • You could try something like:

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

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