ConfigParser: writes a list but reads a string?

Terry Carroll carroll at nospam-tjc.com
Wed Jan 18 01:43:45 EST 2006


On Tue, 17 Jan 2006 09:11:24 -0600, Larry Bates
<larry.bates at websafe.com> wrote:


>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.

Thanks; that's basically pretty close to what I ended up with:

t3string=config2.get("test","testlist")
t3 = [int(x) for x in t3string[1:-1].split(",")]
whatzit("t3", t3)

Gives:

t3 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t3 length: 10
t3 <type 'list'>

i.e., the same as the original list of integers that was written out.

I know, this just assumes the input will be a list in [] brackets,
with no verification of that.  That's okay, because it's just an app
for me, not for any outside user.  If anyone else was using this, I'd
extract the list with a regexp.  

I just want to be able to edit the config file outside of the program
to change things. (Sybren, that's why Pickle was not appealing to me.)

Thanks also to Sybren, Fuzzyman and Frithiof for their contributions.
Nice to know I'm not doing anything wrong, anyway.




More information about the Python-list mailing list