您的位置:首页 > 编程语言 > Python开发

Python标准化 - 使用配置文件

2016-04-18 00:00 531 查看
摘要: Python标准化 - 配置文件

在使用Python进行代码开发的过程中,有一些通用变量建议存放在统一的配置文件中,而此时建议使用ConfigParser实现:

1. 配置文件

[numbers]
N_PI:3.1415926

[messages]
M_WELCOME:Welcome to circle area calculator!
M_DIA:Please enter the circle diameter:
M_RESULT:The circle area is:


2. 示例程序

功能说明:根据输入的直径,计算对应的圆的面积

# coding=utf-8
__metaclass__ = type
__author__ = 'dbloop'

import ConfigParser

l_ConfigFile = r'C:\Python27\configDir\test001\test001.cfg'

# 创建配置文件解析对象
l_CP = ConfigParser.ConfigParser()

# 解析配置文件
l_CP.read(l_ConfigFile)

# 读取配置文件内容

print l_CP.get(section = 'messages',option = 'M_WELCOME')
l_dia = int(raw_input(l_CP.get(section = 'messages',option = 'M_DIA')))

print l_CP.get(section = 'messages',option = 'M_RESULT') + \
str(l_CP.getfloat(section = 'numbers',option = 'N_PI') * l_dia ** 2)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息