[Tutor] embedding lists in configuration files
Alan Gauld
alan.gauld at btinternet.com
Thu Oct 12 23:01:06 CEST 2006
> Python program instead of hard-coded values. The configuration data
> currently is in the form of a couple of lists and some triple-quoted
> strings.
>
> I was looking at the ConfigParser module, but I cannot see how to
> use
> this format to represent lists or triple-quoted strings. Are there
> any
> other suggestions? Thanks.
There are several possibilities:-
1) the simplest is simply a lsingle line
with the values separated by some character - commas, colons,
hyphens, whatever doesn't appear in the data.
Then read the line as a string, split(char) it into its parts and
convert
the parts to the appropriate types. Using commas may allow you
to leverage the CSV module.
2) Use a richer format, like XML which provides for very
complex structures and allows you to use a standard parser
like ElementTree to extract the data.
3) Write all values onm their own lines and add a marker to
separate lists. Then use a while loop to read in the lines until
you reach the marker.
Thee are other options too, but those should be enough
to get you going.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list