Naming dictionaries recursively

Paddy paddy3118 at googlemail.com
Fri Aug 17 14:54:07 EDT 2007


On Aug 17, 3:43 pm, TYR <a.harrow... at gmail.com> wrote:
> > So the tougher problem seems to be parsing those lines.  That is not a
> > valid Python dictionary unless the names `Bob`, `Humboldt`, `red`, and
> > `predatory` are not already defined.  So you can't just ``eval`` it.
>
> In what way? {'key': val}, right?
>
> Anyway, I can always change the format they go into the file in.

If you control the file format then you could create a valid python
list of dictionaries as the intermediate file format. Something like:

  data = [
  {'name' : 'Bob', 'species' : 'Humboldt', 'colour' : 'red',
'habits' : 'predatory'},
  { ... },
  ...
  }

You can then name the file with a .py ending and import it as a list
of dictionaries that you can then process to form a dict of dicts:

  datadict = dict( (data2name(d), d) for d in modulename.data )

- Paddy.






More information about the Python-list mailing list