Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75934

Re: more simple to split the string?

From Peter Otten <__peter__@web.de>
Subject Re: more simple to split the string?
Date 2014-08-09 09:15 +0200
Organization None
References <53E4186A.5020201@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.12785.1407568566.18130.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: more simple to split the string? Peter Otten <__peter__@web.de> - 2014-08-09 09:15 +0200

csiph-web