Python: Split string into multiple lines using delimiter

You can solve it differently. My approach was:

  1. First split the input string by whitespaces and you will get a list
  2. Split the 3rd element of the list by , and get another list.
  3. within a loop, each time make a new copy of the splitted input and append to another list.
  4. Modify 3rd element every inner list items of the bigger lists
  5. Print them as needed

You can look at the below code:

inp="100 name1  a=1,b=2 place1"


splitedWords = inp.split()
result= splitedWords[2].split(",")
lst=[]
for i in range(len(result)):
    lst.append(splitedWords[:])

for i in lst:
    i[2] = result.pop(0)
    print " ".join(i)

Output:

100 name1 a=1 place1
100 name1 b=2 place1

Leave a Comment