How to split strings with multiple delimiters in a list to list items in Python

2 Answers

0 votes
import re

lst = ['python:java;php', 'php*c_sharp:c++,c']
lst = [re.findall('\w+|\W+', s) for s in lst]

print(lst)

for item in lst:
    print(item)


'''
run:

[['python', ':', 'java', ';', 'php'], ['php', '*', 'c_sharp', ':', 'c', '++,', 'c']]
['python', ':', 'java', ';', 'php']
['php', '*', 'c_sharp', ':', 'c', '++,', 'c']

'''

 



answered Nov 13, 2017 by avibootz
0 votes
import re

lst = ['python:java;php', 'php*c_sharp:c++,c']
lst = [re.findall('\w+|\W+', s) for s in lst]

for item in lst:
    for i in item:
        print(i, end=" ")
    print()


'''
run:

python : java ; php
php * c_sharp : c ++, c

'''

 



answered Nov 14, 2017 by avibootz
...