[Tutor] Turn a file into a dictionary

Colin Campbell CCampbell@ede.org
Sat, 28 Sep 2002 15:06:12 -0600


Hello Python gurus!

I have a file containing a series of records. Each record consists of a 
username in the first field, then a comma separated list of a variable 
number of tab names.I optimistically built the file in what I thought 
would be the form of a dictionary:

{user1:tab3,tab5}
{User2:tab1,tab2,tab3,tab4,tab5}

with the intent of simply writing:

f=open('C:\Other\Prg\ACL.CSV','r')
dict=f.readlines()

Regrettably, this turns each record into a string, rather than a 
dictionary entry. My question, then, is: do I need to readline() records 
one by each, parsing the resulting string into a key:(list) sort of form 
or is there a more pythonic way of doing it as a single operation? I 
don't mind record level processing where it's absolutely necessary, but 
if it can be avoided, I find the result cleaner and easier to read.

Any advice or links where I can RTFM will be valued.

rgds,

Colin

--
A closed mouth gathers no feet.
-Unattributed