Created
January 9, 2014 20:17
-
-
Save j-faria/8341187 to your computer and use it in GitHub Desktop.
INI file reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file configfile.py | |
# create a custom INI file reader and read a file called 'test.ini' | |
import ConfigParser | |
class iniReader(ConfigParser.ConfigParser): | |
def as_dict(self): | |
d = dict(self._sections) | |
for k in d: | |
d[k] = dict(self._defaults, **d[k]) | |
d[k].pop('__name__', None) | |
return d | |
cfg = iniReader() | |
filename = 'test.ini' | |
cfg.read(filename) | |
# options is a dictionary -> access variables like options[section][parameter] | |
options = cfg.as_dict() | |
# in test.ini could be something like (remove #s of course) | |
# ; last modified 1 April 2001 by John Doe | |
# [owner] | |
# name=John Doe | |
# organization=Acme Widgets Inc. | |
# [database] | |
# ; use IP address in case network name resolution is not working | |
# server=192.0.2.62 | |
# port=143 | |
# file="payroll.dat" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; last modified 1 April 2001 by John Doe | |
[owner] | |
name=John Doe | |
organization=Acme Widgets Inc. | |
[database] | |
; just another example of a comment | |
server=192.0.2.62 | |
port=143 | |
file="payroll.dat" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment