python解析配置文件

最近有个python小项目,有一堆文件需要处理。所以将文件位置写入配置文件中,顺便写了一个解析配置文件的类,仅供大家参考,需要的同学请拿走


#!/usr/bin/env python
#coding:utf-8

#-----------------------------------------------------
# author: wanglei
# date  : 20160321
# desc  : 解析配置文件
# pram  : 配置文件位置
#-----------------------------------------------------


import ConfigParser

class confParse(object):

    def __init__(self,conf_path):
        self.conf_path = conf_path
        self.conf_parser = ConfigParser.ConfigParser()
        self.conf_parser.read(conf_path)

    def get_sections(self):
        return self.conf_parser.sections()

    def get_options(self,section):
        return self.conf_parser.options(section)

    def get_items(self,section):
        return self.conf_parser.items(section)

    def get_val(self,section,option,is_bool = False,is_int = False):
        if is_bool and not is_int:
            #bool类型配置
            val = self.conf_parser.getboolean(section,option)
            return val
        elif not is_bool and is_int:
            val = self.conf_parser.getint(section,option)
            return val

        val = self.conf_parser.get(section,option)
        return val

配置文件格式如下

[labels_of_search]
base_dir = /home/lei.wang/datas/datas_user_label
cheap = %(base_dir)s/cheap_all
receptions = %(base_dir)s/receptions_all
breakfast = %(base_dir)s/breakfast_all

[result_file]
result_file = /home/lei.wang/datas/datas_user_label/hive_data/user_labels

注意%(xxx)s的用法,xxx需要放在同一个section里


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值