Persuading ConfigParser to give me the section elements in the same order as the file

geoffbache geoff.bache at jeppesen.com
Wed Sep 10 22:52:37 CEST 2008


Hi all,

I recently needed to parse a file that was perfect for ConfigParser
apart from one thing: the elements in the sections, although
definitions, could in some cases clash with each other and therefore
it was important to be able to retrieve them in the same order as they
appeared in the file.

Unfortunately ConfigParser uses ordinary dictionaries for the section
elements and they are therefore returned in an arbitrary order.

The only solution I found was to copy ConfigParser.py and replace all
the dictionaries with "sequential dictionaries"
which are exactly like dictionaries except that elements are returned
in the order they were inserted. (see
http://home.arcor.de/wolfgang.grafen/Python/Modules/seqdict/Seqdict.html)

I wonder if there was a better way? For example, is there any hook
that could modify what is created by the statement

x = {}

I tried setting

__builtins__.dict = ndict.seqdict

But that didn't seem to have any effect on the above statement.

As a secondary question, I find sequential dictionaries to be an
essential part of programming in Python and I use them all the time. I
wondered a bit if there were any plans or proposals to include them as
part of the Python library?

Regards,
Geoff Bache



More information about the Python-list mailing list