[Tutor] Config Files and Thankx (Reading Numerical Data)

Karthikesh Raju karthik at james.hut.fi
Tue Jul 6 12:20:02 CEST 2004

Hi All,

Thankx for the responses for my earlier mail. Somehow, i did not get
the responses, but when i checked the archives i found them. It was
quite useful (Andrei's esp).

Now suppose i have a config file like say

no_of_sources    10
no_of_users      20
result_dir       '/share/wonderful/result/'
antenna          12  # ideally i would want to write no_of_sources+2

no_of_sources     2
no_of_users       4
result_dir        '/share/notsowonderful/result'
antenna           4

Now i want them to be read into two dictionaries

userful, useless with no_of_sources etc as keys and values corresponding
to the RHS entry.  Ofcourse, 10, 20 should be numerals and '/share..' a

To read a single block i am trying something like:

config = {}
f = open('configFileName.ini','r')
for line in f:
    if len(line) > 0 and line[0] != "#" and line[0] != "\n":
             lst = line.split()
             config[lst[0]] = lst[1]

- this can be made to handle two or many blocks too, but

a) How do i convert the numbers to int (numerals) and not string
b) can i do math inside a config file
c) suppose i have 4 sections, and read then in dictionaries as

config{'section1':{},'section2':{}, 'section3':{},'section4':{}}

and call this with:

source, jammer = readConfig('config.file'),

i should be able to get just section1 and section2 in source and jammer

Thankx in advance


Karthikesh Raju,		    email: karthik at james.hut.fi
Researcher,			    http://www.cis.hut.fi/karthik
Helsinki University of Technology,  Tel: +358-9-451 5389
Laboratory of Comp. & Info. Sc.,    Fax: +358-9-451 3277
Department of Computer Sc.,
P.O Box 5400, FIN 02015 HUT,

More information about the Tutor mailing list