Naming dictionaries recursively

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Aug 17 14:59:55 CEST 2007


On Fri, 17 Aug 2007 12:38:16 +0000, TYR wrote:

> I'd like to do something like this; iterate through a file which
> consists of data stored in dictionary format, one dict on each line,
> and read each line into a new dict using one of the values in the dict
> as its name...

Store the dictionaries in a dictionary with that value as key.

> A line from data.txt would look like this: {'name' : Bob, 'species' :
> Humboldt, 'colour' : red, 'habits' : predatory}. Aim is to call one of
> them by name, and merge the values in that dictionary into a string
> pulled from another source.

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.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list