From: joblack on
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
From: Benjamin Kaplan on
On Thu, Jun 3, 2010 at 7:21 PM, joblack <johannes.black(a)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
From: MRAB on
joblack 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
>
Split the string on the semicolons, then split each resulting string on
the equals, then pass the result to dict. You can use a generator
expression for this (or a list comprehension if it's an old version of
Python). If the string has a trailing semicolon then you should strip
that off first.

This all assumes that the values themselves don't contain semicolons or
equals.
From: Bryan on
joblack wrote:
> I've got a string which (without any CR or LF) consists of
>
> 'attribute1=attribute_value;attribute2=attribute_value2; ...'

Technically that's short of a rigorous specification, but it sure
looks like a standard web "query string", the content type known as
"application/x-www-form-urlencoded". See:
http://en.wikipedia.org/wiki/Query_string

> 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?

Parsing query strings is already implemented (more than once) in
Python's standard library. In current Python 2.x, you might use
urlparse.parse_qs(). As in:

>>> from urlparse import parse_qs
>>>
>>> parse_qs('attribute1=attribute_value;attribute2=attribute_value2')
{'attribute2': ['attribute_value2'], 'attribute1':
['attribute_value']}

You'll note the values are lists, to handle the cases where a name is
equated to more than one simple value.


--
--Bryan Olson
From: Tim Chase on
On 06/03/2010 09:21 PM, joblack 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?

While I agree with Bryan that this looks suspiciously like a URL
query-string (and thus you likely want to use his suggestion for
the built-in tools to parse them), I haven't seen the one-liner
version float by, so here it is just for fun:

s = "hello=world;this=that;foo=bar"
results = dict((k,v) for (k,_,v) in (pair.partition('=') for
pair in s.split(';')))

As Bryan cautions, URL query-strings can have multiple values for
the same key, and your example doesn't address that case:

foo=bar;foo=baz;hello=world;this=that

so the code examples you're getting don't address it either :)

-tkc