ConfigParser 读写配置文件

一、ini:

1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式

2.ini文件创建方法:

(1)先建立一个记事本文件。(2)工具 - 文件夹选项 - 查看 - 去掉“隐藏已知文件的扩展名”前面的√。这样一来,你建立的那个记事本的扩展名就显示出来了“*.txt”。然后,你把这个.txt扩展名更改为.ini

3.ini文件的格式:

  (1)节:section

    节用方括号括起来,单独占一行,例如:

    [section]

  (2)键:key

    键(key)又名属性(property),单独占一行用等号连接键名和键值,例如:

    name=value

  (3)注释:comment

    注释使用英文分号(;)开头,单独占一行。在分号后面的文字,直到该行结尾都全部为注释,例如:

    ; comment text

二、ConfigParser

读写配置文件ConfigParser模块是python自带的读取配置文件的模块.通过他可以方便的读取配置文件。

Python的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。

1.读取配置文件

  -read(filename)   直接读取ini文件内容
  -sections()   得到所有的section,并以列表的形式返回
  -options(section)   得到该section的所有option(选项)
  -items(section)   得到该section的所有键值对
  -get(section,option)   得到section中option的值,返回为string类型
  -getint(section,option)   得到section中option的值,返回为int类型

2.写入配置文件

-add_section(section)   添加一个新的section

-set( section, option, value)   对section中的option进行设置

-remove_section(section)                             删除某个 section

-remove_option(section, option)                 删除某个 section 下的 option

需要调用write将内容写回到配置文件。

3.测试代码

(1)配置文件test.cfg

  [sec_a]  

  a_key1 = 20

  a_key2 = 10

  [sec_b]  

  b_key1 = 121

  b_key2 = b_value2

  b_key3 = $r

  b_key4 = 127.0.0.1

(2)测试文件(test.py):

  #生成config对象  

  conf = ConfigParser.ConfigParser()

  #用config对象读取配置文件  

  conf.read("test.cfg")

  #以列表形式返回所有的section  

  sections = conf.sections()

    print ‘sections:‘, sections         #sections: [‘sec_b‘, ‘sec_a‘]

  #得到指定section的所有option  

  options = conf.options("sec_a")

    print ‘options:‘, options           #options: [‘a_key1‘, ‘a_key2‘]

  #得到指定section的所有键值对  

  kvs = conf.items("sec_a")    

    print ‘sec_a:‘, kvs                 #sec_a: [(‘a_key1‘, ‘20‘), (‘a_key2‘, ‘10‘)]

  #指定section,option读取值  

  str_val = conf.get("sec_a", "a_key1")

  int_val = conf.getint("sec_a", "a_key2")

    print "value for sec_a‘s a_key1:", str_val       #value for sec_a‘s a_key1: 20

    print "value for sec_a‘s a_key2:", int_val       #value for sec_a‘s a_key2: 10

  #写配置文件  

  #更新指定section,option的值  

  conf.set("sec_b", "b_key3", "new-$r")

  #写入指定section增加新option和值  

  conf.set("sec_b", "b_newkey", "new-value")

  #增加新的section  

  conf.add_section(‘a_new_section‘)

  conf.set(‘a_new_section‘, ‘new_key‘, ‘new_value‘)

  #写回配置文件  

  conf.write(open("test.cfg", "w"))

原文地址:https://www.cnblogs.com/linbao/p/8323610.html

时间: 2024-08-02 04:24:36

ConfigParser 读写配置文件的相关文章

Python(2.7.6) ConfigParser - 读写配置文件

Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 option: b) section 用 [sect_name] 表示,每个option是一个键值对,使用分隔符 = 或 : 隔开: c) 在 option 分隔符两端的空格会被忽略掉 d) 配置文件使用 # 和 ; 注释 一个简单的配置文件样例 myapp.conf # database source

用ConfigParser模块读写配置文件——Python

对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑的问题. 2)用配置文件,通过在程序中读配置文件获取配置,用户改变配置后重新写入配置文件,即使程序崩溃或者关闭,配置依然能够保存下来. 3)用数据库来存储配置变量,也能长久保存,不过读写数据库也是重量级操作,不太方便. 最近在写一个基于wxPython的GUI程序,需要用到配置文件,本来打算用xml

python之configParser模块读写配置文件

借鉴:http://www.cnblogs.com/TankXiao/p/3038350.html configParser是python自带的模块,用来读写配置文件 配置文件的格式:[]包含的叫section,section下有option=value这样的键值 配置文件  test.conf [section1] name = tank age = 28 [section2] ip = 127.0.0.1 port = 8080 python代码 #-*- coding:UTF-8 -*-

python:实例化configparser模块读写配置文件

之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高了代码的可读性... 1.配置文件 configparser模块支持读取.conf和.ini等类型的文件,在文件夹新建一个.ini文件,写入一些信息,示例如下: config.ini [driver] chromedriver = E:\automation\UI\Testcase\browser\

python-ConfigParser模块【读写配置文件】

http://www.codesky.net/article/201003/122500.html http://www.linuxso.com/linuxbiancheng/8987.html 以下的文章就是对Python 读写配置文件的具体方案的介绍 1,函数介绍 1.1.读取配置文件 -read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section的所有option-items

使用ConfigurationManager类读写配置文件

使用ConfigurationManager类 读写配置文件app.config,以下为代码: view plaincopy to clipboard print? using System; using System.Configuration; static class Program { static void Main() { showConfig(); UpdateAppSettings(); showConfig(); Console.ReadKey(true); } private

使用泛型对读写配置文件的方法进行封装

一般一个站点会有多个配置文件,如果要针对每个配置文件进行读写,这是要疯的节奏 之前学泛型,一直没用到过,在这里练习一下,体会泛型实际对我们减少冗余代码的作用 /// <summary> /// 站点配置文件的路径 /// </summary> public class ConfigPath { /// <summary> /// 数据库配置文件 /// </summary> public static readonly string DB = "/

python ConfigParser模块 配置文件解析

ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic

实现快速读写配置文件的内容,可以用于读取*.exe.config文件或者Web.Config文件的内容,或者可以读取指定文件的配置项.

形如: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microso