Terry Carroll wrote: > It looks like ConfigParser will accept a list to be writing to the > *.ini file; but when reading it back in, it treats it as a string. > > Example: > > ############################### > import ConfigParser > def whatzit(thingname, thing): > print thingname, "value:", thing > print thingname, "length:", len(thing) > print thingname, type(thing) > > cfgfile = "cfgtest.ini" > config1 = ConfigParser.ConfigParser() > config1.add_section("test") > > t1 = range(1,11) > config1.set("test", "testlist", t1) > outfile=open(cfgfile,"w") > config1.write(outfile) > outfile.close() > > config2 = ConfigParser.ConfigParser() > config2.read(cfgfile) > t2 = config2.get("test","testlist") > > whatzit("t1", t1) > whatzit("t2", t2) > > ############################### > > Output is: > > t1 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > t1 length: 10 > t1 <type 'list'> > t2 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > t2 length: 31 > t2 <type 'str'> > > That is, t1 is a list of length 10, consisting of: > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > and is written out; but t2 is read back in as a string > "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" > of length 31. > > It took me a while to figure this out, since they looked identical in > print statements. > > Is there a pythonic way to read in a list from a .INI file with > ConfigParser? Is this expected behavior for ConfigParser? I would > not expect this conversion; rather, an exception when trying to write > the list if the list is not supported. >
To read lists from .INI files I use following: listvalues=INI.get(section, option).split(',') where INI is an instance of ConfigParser There is the problem of if list items contain commas. -Larry Bates -- http://mail.python.org/mailman/listinfo/python-list