datadatadata
datadatadata

Reputation: 323

Python string split multiple brackets

I am looking for a simpler / faster way to split a string by brackets / parenthesis, as well as removing white spaces and useless info.

More specifically, I want to change

[ 5 * * ]{t=0, 1 }{t=0, 3 }{t=0, 2 }

to two part:

 5 * *   (or [ 5 * * ])
['1', '3', '2']

I managed to do this using my code:

test = '[ 5 * * ]{t=0, 1 }{t=0, 3 }{t=0, 2 }  '
parsed =  test.split("[")[1].split(']') 
index = parsed[0]
content = parsed[1].split('{')[1:]
seq=[]
for i in range(len(content)):
    seq.append(content[i][4:-2].replace(' ', ''))   
print index
print seq

which gets:

 5 * * 
['1', '3', '2}']

I'm looking for suggestions to modify my code. It would be ideal if:

  1. There would be no loop.

  2. Less 'split'. (There are three 'split' functions in my code)

  3. More general. (I used content[i][4:-2] to remove '{' and '}' which is not general )

Upvotes: 1

Views: 1187

Answers (1)

Kasravnd
Kasravnd

Reputation: 107297

You can use re.findall and a list comprehension :

>>> l=re.findall(r'\[([^\]]*)\]|,([^}]*)}',s)
>>> [i.strip() for j in l for i in j if i]
['5 * *', '1', '3', '2']

The following regex :

r'\[([^\]]*)\]|,([^}]*)}'

will match everything between brackets (\[([^\]]*)\]) and between comma and } (,([^}]*)}).

Or you can use re.split() :

>>> [i.strip() for i in re.split(r'[{},[\]]',s) if i and '=' not in i]
['5 * *', '1', '3', '2']

Upvotes: 2

Related Questions