python读取配置文件

当需要使用python读取配置文件时,需要使用到python的ConfigParser模块。下面举个栗子。

首先创建init格式的配置文件0.init:

[global]
id = 0
object_x = 0.003

[world]
id = 12
object_x = 0.14

在python中读取并使用配置文件中的内容:

#! /usr/bin/python

import ConfigParser

config = ConfigParser.ConfigParser()
config.readfp(open("0.ini"),"rb")

object_id = int(config.get("global","id"))
object_x = config.get("global","object_x")

print type(object_id), object_id
print type(object_x), object_x

world_id = int(config.get("world","id"))
world_x = config.get("world","object_x")

print type(world_id), world_id
print type(world_x), world_x

输出结果为:

<type 'int'> 0
<type 'str'> 0.003
<type 'int'> 12
<type 'str'> 0.14



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值