views:

30

answers:

2

Let's say that I have the following text:

input = "one aaa and bbb two bbbb er ... // three cccc"

I would like to parse this into a group of variables that contain

criteria = ["one", "two", "three"]
v1,v2,v3 = input.split(criteria)

I know that the example above won't work, but is there some utility in python that would allow me to use this sort of approach? I know what the identifiers will be in advance, so I would think that there has got to be a way to do this...

Thanks for any help, jml

+1  A: 

Not terribly elegant but it works:

>>> s
'one aaa two bbbb three cccc'
>>> re.split(r"\s*(?:one|two|three)\s*", s)
['', 'aaa', 'bbbb', 'cccc']

The ?: keeps it from returning the delimiting identifiers in the results.

John Kugelman
i think this is a decent solution. thank you.
jml
+1  A: 

So, so ugly, but it should do what you need:

i1 = iter(input.split())
i2 = iter(input.split())
next(i2)
strdict = dict(zip(i1, i2))
print operator.itemgetter(*criteria)(strdict)
Ignacio Vazquez-Abrams
very cool. i don't think it's _that_ ugly! i mean; consider this being done in c. :)
jml