questions about how to parse a string and put it in a dictionary

Benjamin Kaplan benjamin.kaplan at case.edu
Thu Jun 3 22:34:46 EDT 2010


On Thu, Jun 3, 2010 at 7:21 PM, joblack <johannes.black at gmail.com> wrote:
> I've got a string which (without any CR or LF) consists of
>
> 'attribute1=attribute_value;attribute2=attribute_value2; ...'
>
> and I want them to read in a dictionary so that the attribute name is
> the key and the attribute value is the data.
>
> Any ideas for an implementation?
>
> Greetings and thanks
> jb
> --

If you can guarantee that the attributes and values don't have
semicolons or equal signs in them, you can just split it

for pair in your_string.split(';') :
    key, value = pair.split('=')
    your_dict[key] = value



More information about the Python-list mailing list