more simple to split the string?

Peter Otten __peter__ at web.de
Sat Aug 9 03:15:50 EDT 2014


elearn wrote:

> str='(\\HasNoChildren \\Junk) "/" "[Gmail]/&V4NXPpCuTvY-"'
> x=str.split(' "')
> [i.replace('"','') for i in x]
> ['(\\HasNoChildren \\Junk)', '/', '[Gmail]/&V4NXPpCuTvY-']
> 
> x.strip(" ") will create four parts.
> 
> is there more simple to do that ?

Here's another way:

>>> s = '(\\HasNoChildren \\Junk) "/" "[Gmail]/&V4NXPpCuTvY-"'
>>> s.replace("(", '"').replace(")", '"').split('"')[1::2]
['\\HasNoChildren \\Junk', '/', '[Gmail]/&V4NXPpCuTvY-']

But you should rather worry about correctness. Can '"' occur inside the 
parentheses and vice versa? Is there a way to escape '"'? Can parentheses be 
nested? Etc.

The example is not sufficient to specify the problem.




More information about the Python-list mailing list