Convert a string to a list

R

rh0dium

Hi all,

I am using pexpect to drive another tool. Some of the data I get back
would be better suited as a list and I want to know a simple way to
parse the data to push it into a list.

For example

I get the following string back. I want to convert this to a list:

'("." ".." "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py")'

should be:
["." ".." "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py"]

It should be able to handle embeded lists like this:
'("." ("cadence.py" "foo_cleanup.py") "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py")'

should become
["." ["cadence.py" "foo_cleanup.py"] "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py"]

Is there some exisitng code which will handle this task?
 
N

Nick Craig-Wood

rh0dium said:
I am using pexpect to drive another tool. Some of the data I get back
would be better suited as a list and I want to know a simple way to
parse the data to push it into a list.

For example

I get the following string back. I want to convert this to a list:

'("." ".." "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py")'

should be:
["." ".." "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py"]

It should be able to handle embeded lists like this:
'("." ("cadence.py" "foo_cleanup.py") "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py")'

should become
["." ["cadence.py" "foo_cleanup.py"] "cdslib_cleanup.py" "cadence.py"
"cdsinit_cdsenv_cleanup.py"]

Here is a cheap, nasty and insecure solution

It made tuples rather than lists but I expect that won't matter.

Someone normally chimes in with pyparsing at this point...
 
P

Paul McGuire

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"']
 
P

Paul McGuire

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"']

Oh, you probably don't want those quotation marks in the parsed
strings. Change:

pp.ZeroOrMore( pp.quotedString | pp.Group(list_) ) +
to:


pp.ZeroOrMore( pp.quotedString.setParseAction(pp.removeQuotes) |
pp.Group(list_) ) +


-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top