Create list/dict from string

Peter Otten __peter__ at web.de
Tue Jan 19 17:57:46 CET 2010


SoxFan44 wrote:

> I was wondering if there was a way to create a list (which in this
> case would contain several dicts) based on a string passed in by the
> user.  Security is not an issue.  Basically I want to be able to have
> the user pass in using optparse:
> --actions=[{"action_name": "action_1", "val": "asdf", "val2":
> "asdf"},
>                {"action_name": "action_2", "val": "asdf", "val2":
> "asdf"},
>                {"action_name": "action_1", "val": "asdf", "val2":
> "asdf"}]
> 
> And have this create a list/dict.  I'm aware of pickle, but it won't
> work as far as I can tell.

Both eval() and json.loads() will do. eval() is dangerous as it allows the 
user to run arbitrary python code.

Peter



More information about the Python-list mailing list