`
mingren135
  • 浏览: 69080 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Python-ConfigParser读取ini文件

 
阅读更多

今天迎来了2013的第一场大雪,离2002年的第一场雪已经过去11年了,真真切切的感受到什么是弹指一挥间。最近工作中用到了CP模块,顺便就总结一下。

 

#!/usr/bin/env python
import ConfigParser as cp

def read(cf):
    for section in cf.sections():
        print '[' + section + ']'
        for option in cf.options(section):
            print option + "=" + cf.get(section,option)

if __name__== '__main__':

    cf = cp.ConfigParser()
    cf.read('aa.ini')

    ##Read
    read(cf)
    ##Modify
    cf.add_section('test')
    cf.set('test', 'key1', 'value1')
    cf.set('test', 'key2', 'value2')
    ##Read
    read(cf)
    ##Write
    cf.write(open("aa.ini", "w")) 
 

1)常见的方法列表,如dir(ConfigParser.ConfigParser):

 

['OPTCRE', 'OPTCRE_NV', 'SECTCRE', '_KEYCRE', '__doc__', '__init__', '__module__', '_boolean_states'

, '_get', '_interpolate', '_interpolation_replace', '_read', 'add_section', 'defaults', 'get', 'getb

oolean', 'getfloat', 'getint', 'has_option', 'has_section', 'items', 'options', 'optionxform', 'read

', 'readfp', 'remove_option', 'remove_section', 'sections', 'set', 'write']

 

 

2)ConfigParser模块包含3个类:ConfigParser、RawConfigParser、SafeConfigParser,RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。 当配置文件有些特殊字符时,需要用到RawConfigParser,这里我遇到的情况是日志的配置

 

    ##RawConfigParser
    cf = cp.ConfigParser()
    cf.read('bb.ini')
    read(cf)

    cf = cp.RawConfigParser()
    cf.read('bb.ini')
    read(cf)

[website]
url=http://localhost:80/
host=localhost
port=80
[website]
url=http://%(host)s:%(port)s/
host=localhost
port=80
 

 

分享到:
评论

相关推荐

    Python使用configparser读取ini配置文件

    主要介绍了Python使用configparser读取ini配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Python3中configparser模块读写ini文件并解析配置的用法详解

    主要介绍了Python3中configparser模块读写ini文件并解析配置的用法详解,需要的朋友可以参考下

    [python] python编程之ini文件处理-configparser模块应用

     2.1 读取 ini 配置文件  存在config.ini配置文件,内容如下: #config.ini [DEFAULT] excel_path = ../test_cases/case_data.xlsx log_path = ../logs/test.log log_level = 1 [email] user_name = 32@qq.com ...

    Python configparser模块常用方法解析

    ConfigParser模块在python中用来读取配置文件,配置文件的格式跟...read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取ini文件内所有的section,以列表形式返回[‘logging’, ‘mysql’] optio

    python读取ini配置文件过程示范

    这篇文章主要介绍了python读取ini配置文件过程示范,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 pip install configparser 1 配置文件 config.ini: ...

    Python使用自带的ConfigParser模块读写ini配置文件

    ini文件 ini配置文件格式: 读取配置文件: import ConfigParser conf = ConfigParser.ConfigParser() conf.read('dbconf.ini') # 文件路径 name = conf.get("section1", "name") # 获取指定section 的option值 ...

    Python文件去除注释的方法

    本文实例讲述了Python文件去除注释的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/python ... #读取ini cf=ConfigParser.ConfigParser() cf.read(path) value=cf.get(section,opt

    Python ConfigParser模块的使用示例

    该模块适用于配置文件的格式与windows ini文件类似,是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。格式如下: [DEFAULT] ...

    python读取配置文件方式(ini、yaml、xml)

    零、前言 python代码中配置文件是必不可少的内容。常见的配置文件格式有很多中:ini、yaml、xml、properties、txt、py等。...python自带的configparser模块可以读取.ini文件,注意:在python2中是ConfigParser 创建

    Python中的ConfigParser模块使用详解

     -read(filename) 直接读取ini文件内容  -sections() 得到所有的section,并以列表的形式返回  -options(section) 得到该section的所有option  -items(section) 得到该section的所有键值对  -get(section,...

    Java_Properties_类读取配置文件信息

    因为他内部有一个ConfigParser 类来支持.ini 文件的读写,根据该类提供的方法程序员可以自由的来操作.ini 文件。而在Java 中,Java 支持的是.properties 文件的读写。JDK 内置的java.util.Properties 类为我们操作....

    Python configparser模块应用过程解析

    2.1 读取 ini 配置文件 #存在 config.ini 配置文件,内容如下: [DEFAULT] excel_path = ../test_cases/case_data.xlsx log_path = ../logs/test.log log_level = 1 [email] user_name = 32@qq.com password = ...

    详解Python读取配置文件模块ConfigParser

    假设有如下配置文件,需要在Pyhton程序中读取 $ cat config.ini [db] db_port = 3306 db_user = root db_host = 127.0.0.1 db_pass = xgmtest [SectionOne] Status: Single Name: Derek Value: Yes Age: 30 Single:...

    configupdater::palm_tree:解析器,类似于ConfigParser,但用于更新配置文件

    因此ConfigUpdater提供互补的功能Python的这主要是为了读取配置文件,并写入新的。 特征 的主要区别在于: 更新配置文件中的侵入性最小的更改, 正确处理评论, 一次只能更新一个配置文件, 保留了部分和键的...

    python配置文件写入过程详解

    python集成的 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件 我的配置文件如下 [MOTOR] comnum = 3 baud = 19200 m1slowstep = 10 m1faststep = 100 m1origin = 5 m2slowstep = 10 m2faststep = 50...

    gzhihu:gzhihu是一个从知乎上爬取内容的爬虫

    conf.read('conf.ini') #读取当前目录下conf.ini文件 email = conf.get('account', 'email') #得到account段中email的value conf.section() #执行结果为一个列表,包含3个段的段名信息 ['account', 'cookies', '...

Global site tag (gtag.js) - Google Analytics