[Tutor] string-formatted dictionary keys of configparsed INI file

Eur van Andel eur at fiwihex.nl
Thu Nov 13 20:33:42 EST 2003


Hi

I managed to parse an ini file with the configparser thanks to Karl who pointed
me at an example in the cookbook:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65334

I have the values as integers, just as I want, but the dictionary keys are
strings:

>import ConfigParser
>import string
>
>_ConfigDefault = {}
>
>def LoadConfig(file, config={}):
>   """
>   returns a dictionary with key's of the form
>   <section>.<option> and the values 
>   """
>   config = config.copy()
>   cp = ConfigParser.ConfigParser()
>   cp.read(file)
>   for sec in cp.sections():
>      name = sec
>      for opt in cp.options(sec):
>         config[name + "." + opt] = int(cp.get(sec, opt).strip())
modified from example to get int() values. 

>   return config
>
>if __name__=="__main__":
>   cf = LoadConfig("config.ini", _ConfigDefault)
>   print cf
>
>   for n in range(cf['main.number_of_fans']):
>      print cf['fan' + '%1d' % n+'.dtai']

While this does work (ini file is posted below) I would like to address the
values as:

	cf[fan0.dtai]

instead of 

	cf['fan0.dtai']

What did I do wrong?


INI file:
>;
>;     config.ini
>;
>; comment starts with a ;
>
>[main]
>pump_address =      1
>number_of_boards =  8
>number_of_fans =    6
>control_address =   8 
>
> 
>[CO2]
>sollwert =         800
>
>[pump1]
>max_speed =        100
>manual =           0
>manual_speed =     100
>
>[pump2]
>max_speed =        100
>manual =           0
>manual_speed =     100
>
>[temps]
>cool =             30
>heat =             15
>
>; values for all fans
>
>[fans]
>manual =           0
>manual_speed =     100
>
>; values for specific fans, fans numbered from 0..number_of_fans - 1
>
>[fan0]
>dTai =             -2
>dTao =             +3
>dTwl =             -1
>dTwh =             0
>
>
>[fan1]
>dTai =             1
>dTao =             -2     
>dTwl =             3
>dTwh =             -4
>
>[fan2]
>dTai =             0
>dTao =             0     
>dTwl =             0
>dTwh =             0
>
>[fan3]
>dTai =             0
>dTao =             0     
>dTwl =             0
>dTwh =             0
>
>[fan4]
>dTai =             0
>dTao =             0     
>dTwl =             0
>dTwh =             0
>
>[fan5]
>dTai =             0
>dTao =             0     
>dTwl =             0
>dTwh =             0
>
>[control_group]
>dTai =             0
>dTao =             0
>dTwl =             0
>dTwh =             0
>

--
Ir. E.E. van Andel, Fine Wire Heat Exchangers, Fiwihex B.V. www.fiwihex.com
Wierdensestraat 74, NL-7604 BK  Almelo, The Netherlands   eur at fiwihex.nl
phone +31-546-491106  fax +31-546-491107  mobile +31-653-286573  



More information about the Tutor mailing list