parse a string of parameters and values

John Machin sjmachin at lexicon.net
Sun Dec 13 00:52:04 EST 2009


Steven D'Aprano <steve <at> REMOVE-THIS-cybersource.com.au> writes:

> 
> On Sat, 12 Dec 2009 16:16:32 -0800, bsneddon wrote:
> 

> 
> > I am going to read a text file that is an export from a control system.
> > It has lines with information like
> > 
> > base=1 name="first one" color=blue
> > 
> > I would like to put this info into a dictionary for processing. 
> 
> Have you looked at the ConfigParser module?
> 
> Assuming that ConfigParser isn't suitable, you can do this if each 
> key=value pair is on its own line:
> [snip]
> If you have multiple keys per line, you need a more sophisticated way of 
> splitting them. Something like this should work:
> 
> d = {}
> for line in open(filename, 'r'):
>     if not line.strip():
>         continue
>     terms = line.split('=')
>     keys = terms[0::2]  # every second item starting from the first
>     values = terms[1::2]  # every second item starting from the second
>     for key, value in zip(keys, values):
>         d[key.strip()] = value.strip()
> 

There appears to be a problem with the above snippet, or you have a strange
interpretation of "put this info into a dictionary":

| >>> line = 'a=1 b=2 c=3 d=4'
| >>> d = {}
| >>> terms = line.split('=')
| >>> print terms
| ['a', '1 b', '2 c', '3 d', '4']
| >>> keys = terms[0::2]  # every second item starting from the first
| >>> values = terms[1::2]  # every second item starting from the second
| >>> for key, value in zip(keys, values):
| ...     d[key.strip()] = value.strip()
| ...
| >>> print d
| {'a': '1 b', '2 c': '3 d'}
| >>>

Perhaps you meant

terms = re.split(r'[= ]', line)

which is an improvement, but this fails on cosmetic spaces e.g. a = 1  b = 2 ...

Try terms = filter(None, re.split(r'[= ]', line))

Now we get to the really hard part: handling the name="first one" in the OP's
example. The splitting approach has run out of steam.

The OP will need to divulge what is the protocol for escaping the " character if
it is present in the input. If nobody knows of a packaged solution to his
particular scheme, then he'll need to use something like pyparsing.







More information about the Python-list mailing list