usk70 usk70 - 1 month ago 8
Python Question

Parse string in python

I would like to turn this:

mystr = ' foo1 (foo2 foo3 (foo4))'


into:

['foo1','foo2 foo3 (foo4)']


So basically I have to split based on a number of spaces/tabs and parenthesis.

I have seen that re package split function can handle several delimiters (Python: Split string with multiple delimiters) but I can not manage to understand the right approach to parse this kind of strings.

Which would be the best -most pythonic- and simple approach?

Answer Source

As far as I can understand, this is consistent with what you want, and is pretty simple. It just uses some slicing to isolate the first word and the part between parentheses. It also has to use strip a couple of times due to the extra spaces. It may seem a little verbose, but to be honest if the task can be accomplished with such simple string operations I feel like complicated parsing is unnecessary (although I may have gotten it wrong). Note that this is flexible in the amount of whitespace to split by.

mystr = '  foo1   (foo2 foo3 (foo4))' 
mystr = mystr.strip()
i = mystr.index(' ')
a = mystr[:i].strip()
b = mystr[i:].strip()[1:-1]
print([a, b])

with output

['foo1', 'foo2 foo3 (foo4)']

Although I'm still not entirely clear if this is what you want. Let me know if it works or what needs changing.