configparser - which one?

Dave dboland9 at offilive.com
Mon Mar 25 20:10:40 EDT 2019


I use Python3 3, and expected learning how to use configparser would be 
no big deal.  Well!  Seems there is configparser, stdconfigparser, and 
safeconfigparser, and multiple ways to set the section and entries to 
the section.  A little confusing.  I want to future-proof may code, so 
what should I be using?

As for setting the sections and entries, the following both work.

from configparser import ConfigParser	# Python 3 syntax
parser = ConfigParser()

parser['DEFAULT'] = {'Language': 'English',
                      'Units': 'English',
                      'UseDefaults': 'True',
                      'NumberRidesDisplay': '30',
                      'Pi': '3.14'}


parser.add_section('Default')
parser.set('default', 'language', 'english')
parser.set('default', 'units_measure', 'english')
parser.set('default', 'background_color', 'white')
parser.set('default', 'useDefaults', 'true')
parser.set('default', 'numToDisp', '12')
parser.set('default', 'pi', '3.14')

The advantage of the former is that it will handle 'DEFAULT', while the 
last one won't.  I like the former, but not sure if it is the future.

Thanks,
Dave



More information about the Python-list mailing list