Convert a string to a list
Paul McGuire
ptmcg at austin.rr.com
Wed Apr 25 22:02:49 EDT 2007
On Apr 24, 12:30 pm, Nick Craig-Wood <n... at craig-wood.com> wrote:
>
> Someone normally chimes in with pyparsing at this point...
>
Well it *is* a short pyparsing routine, after all...
-- Someone
tests = """\
("." ".." "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py")
("." ("cadence.py" "foo_cleanup.py") "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py") """.splitlines()
import pyparsing as pp
LPAR,RPAR = map(pp.Suppress,"()")
list_ = pp.Forward()
list_ << ( LPAR +
pp.ZeroOrMore( pp.quotedString | pp.Group(list_) ) +
RPAR )
for t in tests:
result = list_.parseString(t)
print result.asList()
Prints:
['"."', '".."', '"cdslib_cleanup.py"', '"cadence.py"',
'"cdsinit_cdsenv_cleanup.py"']
['"."', ['"cadence.py"', '"foo_cleanup.py"'], '"cdslib_cleanup.py"',
'"cadence.py"', '"cdsinit_cdsenv_cleanup.py"']
More information about the Python-list
mailing list