[Tutor] creating variable names from string slices

Bruce Dykes Bruce Dykes" <bkd@graphnet.com
Tue Apr 1 10:01:02 2003

----- Original Message -----
From: <alan.gauld@bt.com>
To: <bkd@graphnet.com>; <tutor@python.org>
Sent: Tuesday, April 01, 2003 08:05
Subject: RE: [Tutor] creating variable names from string slices

> > Betty     Blonde    Student
> > Veronica  Brunette  Student
> >
> > What I want to do is define a function that gets passed each line, and
> > returns a dictionary with the first element  as the name, and
> > the subsequent elements as the key:value pairs:
> Split the line then assign the firstvelement as the key and the rest as
> value.
> Something like:
> words = line.split()
> data[words[0]] = words[1:]
> > How do I make this trick work?
> Split the string rather than using slicing. You probably want to strip()
> any whitespace off the words before inserting them too...

Hrrmm....I've actually decided to follow Michael's advice, as the
dictionaries will be bundled up into a list anyway, and it really doesn't
matter if the list is [a, b, c] or [{'name':'a'}, {'name':'b'},
{'name':'c'}]. Or does it?

But back to your solution, the data is columnar, and some of the fields are
empty, so I think I would have to modify your snippet above: