python模块之ConfigParser:解析配置文件模块

来源:互联网 发布:淘宝怎么设置促销价格 编辑:程序博客网 时间:2024/05/18 04:54

python模块之ConfigParser:解析配置文件模块

ConfigParser是一个简单易用的python解析配置文件的模块,可以从配置文件中解析出各section,各种类型的参数。

配置文件

# cat sample.conf

[numbers]pi = 3.141592653[messages]greeting = Welcome to the area calculation program!question = Please enter the radius:result_message = The area is

解析代码

# cat parseconfig.py

#!/usr/bin/env pythonimport ConfigParserCONFIGFILE = 'sample.conf'config = ConfigParser.ConfigParser()#Read config fileconfig.read(CONFIGFILE)print config.get('messages', 'greeting')radius = input(config.get('messages', 'question') + ' ')print config.get('messages', 'result_message'),print config.getfloat('numbers', 'pi') * radius**2