Noob Parsing question

Chris Angelico rosuav at gmail.com
Tue Feb 17 23:16:05 EST 2015


On Wed, Feb 18, 2015 at 3:07 PM,  <kai.peters at gmail.com> wrote:
> Given
>
> data = '{[<a=14^b=Fred^c=45.22^><a=22^b=Joe^><a=17^c=3.20^>][<a=72^b=Soup^>]}'
>
> How can I efficiently get dictionaries for each of the data blocks framed by <> ?
>
> Thanks for any help

The question here is: What _can't_ happen? For instance, what happens
if Fred's name contains a greater-than symbol, or a caret?

If those absolutely cannot happen, your parser can be fairly
straight-forward. Just put together some basic splitting (maybe a
regex), and then split on the caret inside that. Otherwise, you may
need a more stateful parser.

ChrisA



More information about the Python-list mailing list