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 [0.05] 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() parser.read("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 value = pair # 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 parser.read(“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.