The Python configparser: a way to read simple data files

My simulation library, which is written mostly in Python, needs a lot of data and parameters in order to run. In some cases, I just hard-code the values in the script that calls the library, and in other cases I load a pickle file containing a Python objext. What if I want to read in data or configuration parameters from a human-editable text file? If the information is extensive or complex, XML might be a good choice, but XML is overkill for simple configuration or data files. Fortunately, a standard Python library called ConfigParser has already defined a configuration file format, and provides methods to interact with such files. Here is a sample of the format used in a config file:

[0.01]         ; this is a comment
1.0: 27.221
2.0: 4.214
5.0: 0.6973
10.0: 0.1523
30.0: -0.00120
1.0: 325.586
2.0: 30.858
5.0: 2.376
10.0: 0.458
30.0: -0.0159

Reading such a file is easy:

parser = ConfigParser.SafeConfigParser()"filename")
section_names = parser.sections() # returns a list of strings of section names
items_in_section = parser.items(section_names[i]) # get a list of tuples from a section
pair = parser.items(section_names[i])[j]  # get one key/value pair
key = pair[0]
value = pair[1]     # values are always strings--remember to convert if needed

There are a couple of interesting “features” you should know about. First, if you open a file using the“filename”) method, no exception will be raised if the file does not exist! If reading the file is not optional, first open the file with the fp=open(“filename”) and then call parser.readfp(fp). The second caveat is that the sections or key/value pairs returned from the parser are not necessarily returned in the order that they exist in the file. If you are trying to read data into arrays, be aware that the arrays may not be in order, so you may need to sort them.
Why reinvent the wheel when trying to do simple things? Unless you are doing it as an exercise, just look through the Python library and you’ll probably find that someone has already done the work for you.

3 thoughts on “The Python configparser: a way to read simple data files”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.