parsing string into dict

Arnaud Delobelle arnodel at googlemail.com
Wed Sep 1 16:20:07 EDT 2010


Tim Arnold <a_jtim at bellsouth.net> writes:

> Hi,
> I have a set of strings that are *basically* comma separated, but with
> the exception that if a comma occur insides curly braces it is not a
> delimiter.  Here's an example:
>
> [code=one, caption={My Analysis for \textbf{t}, Version 1}, continued]
>
> I'd like to parse that into a dictionary (note that 'continued' gets
> the value 'true'):
> {'code':'one', 'caption':'{My Analysis for \textbf{t}, Version
> 1}','continued':'true'}
>
> I know and love pyparsing, but for this particular code I need to rely
> only on the standard library (I'm running 2.7). Here's what I've got,
> and it works. I wonder if there's a simpler way?
> thanks,
> --Tim Arnold
>

FWIW, here's how I would do it:

def parse_key(s, start):
    pos = start
    while s[pos] not in ",=]":
        pos += 1
    return s[start:pos].strip(), pos

def parse_value(s, start):
    pos, nesting = start, 0
    while nesting or s[pos] not in ",]":
        nesting += {"{":1, "}":-1}.get(s[pos], 0)
        pos += 1
    return s[start:pos].strip(), pos

def parse_options(s):
    options, pos = {}, 0
    while s[pos] != "]":
        key, pos = parse_key(s, pos + 1)
        if s[pos] == "=":
            value, pos = parse_value(s, pos + 1)
        else:
            value = 'true'
        options[key] = value
    return options

test = "[code=one, caption={My Analysis for \textbf{t}, Version 1}, continued]"

>>> parse_options(test)
{'caption': '{My Analysis for \textbf{t}, Version 1}', 'code': 'one', 'continued': True}

-- 
Arnaud



More information about the Python-list mailing list