Parsing Search Criteria

Greg Krohn ('rot-13') 'tert at pncen.hf'.decode
Thu Mar 6 20:35:02 EST 2003


"Alex Martelli" <aleax at aleax.it> wrote in message
news:veQ9a.22161$zo2.646805 at news2.tin.it...
> >>>> parse_criteria('parrot "I like Python." ternary operator trouble')
> > ['I like Python.', 'parrot', 'ternary', 'operator', 'trouble']
>
> I'm confused by the strange ordering you specify in this example.
> Without strange reordering, here's how I'd do it:

The order has no significance. It's that way, only because I pulled the
quoted words out first, then dealt with the other words.

> import tokenize, cStringIO
> def parse_criteria(criteria):
>     rl = cStringIO.StringIO(criteria).readline
>     return [ t[1] for t in tokenize.generate_tokens(rl) if t[0] ]

Perfect! Now I just need to go read the tokenize docs.


Thank you,
greg






More information about the Python-list mailing list