Python: Read configuration file with multiple lines per key Python: Read configuration file with multiple lines per key sql sql

Python: Read configuration file with multiple lines per key


The Python standard library module ConfigParser supports this by default. The configuration file has to be in a standard format:

[Long Section]short: this is a normal linelong: this value continues    in the next line

The configuration file above could be read with the following code:

import ConfigParserconfig = ConfigParser.ConfigParser()config.read('longsections.cfg')long = config.get('Long Section', 'long')


This is almost exactly the use-case that made us switch to YAML (Wikipedia, python implementation, documentation; you might want to look at JSON as an alternative). YAML has some advantages over configparser or json:

  • human readability (better than JSON for larger files);
  • can serialize arbitrary python objects (which makes it as un-safe as pickle, but there is a safe_load function in the python implementation to alleviate this issue). This is already useful for something as simple as a datetime object.

For completeness sake, the main disadvantages (IMO):

  • Python implementation by an order of magnitude slower than JSON implementation;
  • less portable across platforms than JSON.

For example

import yamlsql = """query         : "SELECT * from citiesWHERE name='Unknown';"count         : 0level         : 1name          : "Check for cities whose name should be null"suggested_fix : "UPDATE cities SET name=NULL WHERE name='Unknown';""""sql_dict = yaml.safe_load(sql)print(sql_dict['query'])

prints

SELECT * from cities WHERE name='Unknown';


I would you suggest to use a regular expression... The code might look like this to give you are start:

import retest="""query = "select * from cities;"count = 0multine_query = "select *from cities     where name='unknown';""""re_config = re.compile(r'^(\w+)\s*=\s*((?:".[^"]*")|(?:\d+))$', re.M)for key, value in re_config.findall(test):    if value.startswith('"'):        value = value[1:-1]    else:        value = int(value)    print key, '=', repr(value)

The output of this example is:

~> python test.py query = 'select * from cities;'count = 0multine_query = "select *\nfrom cities\n     where name='unknown';"

Hope that helps!

Regards,Christoph