parse a string of parameters and values

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Dec 14 19:23:38 EST 2009


En Sun, 13 Dec 2009 07:28:24 -0300, Peter Otten <__peter__ at web.de>  
escribió:
> 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.
>
>>>> import shlex
>>>> s = 'base=1 name="first one" color=blue equal="alpha=beta" empty'
>>>> dict(t.partition("=")[::2] for t in shlex.split(s))
> {'color': 'blue', 'base': '1', 'name': 'first one', 'empty': '', 'equal':
> 'alpha=beta'}

Brilliant!

-- 
Gabriel Genellina




More information about the Python-list mailing list