ConfigParser & converting strings to lists

Edward K. Ream edream at tds.net
Sat Jun 22 22:33:00 EDT 2002


> > so it is strings of the form "['file1' 'file2' ...]" that must be
> > converted to a list of strings.
> 
> >>> s = "['file1' 'file2' '...']"
> >>> [x[1:-1] for x in s[1:-1].split()]
> ['file1', 'file2', '...']
> 
> Does that do it?

I'm not sure.  Here is what I did:

s = config.get(section,"recentFiles")
files = [x[1:-1] for x in s[1:-1].split()]

Is this what you intended?  This doesn't work for me: it "doubles" every
backspace.

I am already regretting trying to be clever.  It is easy enough to set
file individual filenames like this:

for i in xrange(len(files)):
  config.set(section, "file"+`i`, files[i])

and to get up to n file names like this:

try:
  for i in xrange(n):
    files.append(config.get(section, "file" + `i`))
except: pass

This is bulletproof: filenames can contain any character at all.  The
only drawback is that the config file looks like:

[recent files]
file0 = filename0
file1 = filename1
...

But this isn't so bad, is it?

Edward
--------------------------------------------------------------------
Edward K. Ream   email:  edream at tds.net
Leo: Literate Editor with Outlines
Leo: http://personalpages.tds.net/~edream/front.html
--------------------------------------------------------------------



More information about the Python-list mailing list