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

J

joblack

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
 
B

Benjamin Kaplan

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
 
M

MRAB

joblack said:
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.
 
B

Bryan

joblack said:
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:
{'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.
 
T

Tim Chase

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top