469,964 Members | 1,606 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

Convert a string to a list

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?

Apr 24 '07 #1
3 1694
rh0dium <st**********@gmail.comwrote:
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
>>a='("." ".." "cdslib_cleanup.py" "cadence.py" "cdsinit_cdsenv_cleanup.py")'
eval(a.replace('" "', '", "').replace('" (', '", (').replace(') "', '), "'))
('.', '..', 'cdslib_cleanup.py', 'cadence.py', 'cdsinit_cdsenv_cleanup.py')
>>b='("." ("cadence.py" "foo_cleanup.py") "cdslib_cleanup.py" "cadence.py" "cdsinit_cdsenv_cleanup.py")'
eval(b.replace('" "', '", "').replace('" (', '", (').replace(') "', '), "'))
('.', ('cadence.py', 'foo_cleanup.py'), 'cdslib_cleanup.py', 'cadence.py', 'cdsinit_cdsenv_cleanup.py')
>>>
It made tuples rather than lists but I expect that won't matter.

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

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Apr 24 '07 #2
On Apr 24, 12:30 pm, Nick Craig-Wood <n...@craig-wood.comwrote:
>
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"']

Apr 26 '07 #3
On Apr 25, 9:02 pm, Paul McGuire <p...@austin.rr.comwrote:
On Apr 24, 12:30 pm, Nick Craig-Wood <n...@craig-wood.comwrote:
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

Apr 26 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andrew V. Romero | last post: by
2 posts views Thread by Steve | last post: by
9 posts views Thread by myotheraccount | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.