python configure

configuration文件命名为*.conf

1.import ConfigParser

2.初始化 cf=ConfigParser.ConfigParser()

3.获取section集合 secs=cf.sections()

4.通过每个sec,获取其options & items

5.进行处理


import ConfigParser

def read2header(file):
        headermap={}

        headerconfig=ConfigParser.ConfigParser()
        #read config
        headerconfig.read(file)

        #return secions
        secs=headerconfig.sections()
        #print "sections: ",secs

        for sec in secs:
                headermap[sec]={}
                items = headerconfig.items(sec)
                #print 'items:', items 

                for item in items:
                        headermap[sec][item[0]]=item[1]
        return headermap



你可能感兴趣的:(python)