conver string to dictionary

Daniel Nogradi nogradi at gmail.com
Sun Feb 18 03:32:28 EST 2007


>   I want to convert string to dictionary .what is the best solution for this?
>   for example string is like this:
>
>
> '{"SalutationID":["primarykey",8388607,0,None],"CompanyID":[0,8388607,0,"index"],
>  "SalutationName":["",255,0,None],"isDefault":["tinyint",1,1,None]}'
>
>   and I want to convert this string to this dictionary:
>
>
> {"SalutationID":["primarykey",8388607,0,None],"CompanyID":[0,8388607,0,"index"],
>  "SalutationName":["",255,0,None],"isDefault":["tinyint",1,1,None]}
>
>   please help me what is the best solution(faster solution) for this?


If you have simplejson  ( http://cheeseshop.python.org/pypi/simplejson
) installed you can do
mydict = simplejson.loads( mystring )
but in this case all occurances of 'None' in your string should be 'null'.

Or if you absolutely trust the string to contain nothing dangerous:
exec( 'mydict = %s' % mystring )

HTH,
Daniel



More information about the Python-list mailing list