1.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类型

1.2 写入配置文件

-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置
  需要调用write将内容写入配置文件。

1.3 命令执行展示

config.ini内容如下:

[auth]
accesskey = HK6ICA0098BMC9421NMD
secretkey = BdbkPkgS0gJ586i9325i1rB10gZSIkW4i7uIsRYO [api]
server = 192.168.8.56:8080
server = 192.168.8.56:8080
version = V1
default_regin = wuli11-777 [compute]
default_vpcid=989263d7-e897-4f0e-af6f-751a69def74c
default_msub_netid=589fdd7f-91a8-42a4-9fe9-add81448ef32
default_image_id=cef886fc-4b88-4f5d-980b-2df2b7e38f6b
default_cpu_count=1
default_mem_size=1
default_login_mode=passwd
default_password=Pass1115word
default_instance_name=name
default_pay_type=Pay_Dynamic [shutdown]
default_instanceid=9d9f777e-9bbc-4ce8-91d2-638dc756aa3a

#导入ConfigParser模块

>>> import ConfigParser

#生存cf对象

>>> cf = ConfigParser.ConfigParser()
#读取配置文件
>>> cf.read("config.ini")
['config.ini']

#以列表方式返回所有的section
>>> sections = cf.sections()
>>> print 'sections:', sections
sections: ['auth', 'api', 'compute', 'shutdown']

#获取指定section的所有option值
>>> options = cf.options("auth")
>>> print 'options:',options
options: ['accesskey', 'secretkey']

#获取指定section所有的option的键值对
>>> valus = cf.items('auth')
>>> print valus
[('accesskey', 'HK6ICA0098BMC9421NMD'), ('secretkey', 'BdbkPkgS0gJ586i9325i1rB10gZSIkW4i7uIsRYO')]

#获取指定section,option的值,且值的类型为string
>>> valus1 = cf.get('auth','accesskey')
>>> print valus1
HK6ICA0098BMC9421NMD

#获取指定section,option的值,且值的类型为int
>>> valus2 = cf.getint('compute','default_cpu_count')
>>> print valus2

#更新指定section,option的值,如果option不存在,直接新增option以及对应的值

>>> cf.set('compute','default_cpu_count','2')
>>> cf.set('compute','name','luolijun')

#新增section test
>>> cf.add_section('test')

#给新增的section test增加option以及对应的值
>>> cf.set('test','test_name','zhangsan')

#将更改写入到配置文件中
>>> cf.write(open('config.ini','w'))

python 之ConfigParser模块学习的更多相关文章

  1. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  2. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  3. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

  4. python中configparser模块读取ini文件

    python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...

  5. python 的ConfigParser模块

    Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

  6. Python - configParser模块学习

    configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...

  7. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  8. 【Python】logging模块学习笔记

    因为做接口自动化测试遇到的一个代码逻辑上的问题,又不知道具体问题出在哪里,所以在模块化代码之前,先学习下python的日志模块logging. 入门1 入门2 日志级别大小关系为:CRITICAL & ...

  9. python封装configparser模块获取conf.ini值

    configparser模块是python自带的从文件中获取固定格式参数的模块,因为是python只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...

随机推荐

  1. 【uoj#175】新年的网警 结论题+Hash

    题目描述 给出一张 $n$ 个点 $m$ 条边的无向连通图,每条边的边权为1.对于每个点 $i$ ,问是否存在另一个点 $j$ ,使得对于任意一个不为 $i$ 或 $j$ 的点 $k$ ,$i$ 到 ...

  2. BZOJ5072 小A的树(树形dp)

    容易猜到能选择的黑点个数是一个连续区间.那么设f[i][j]为i子树内选j个点形成包含根的连通块,最多有几个黑点,g[i][j]为最少有几个黑点,暴力dp是O(n2)的,求出每个连通块大小对应的黑点数 ...

  3. [AT2369] [agc013_c] Ants on a Circle

    题目链接 AtCoder:https://agc013.contest.atcoder.jp/tasks/agc013_c 洛谷:https://www.luogu.org/problemnew/sh ...

  4. Spring Cloud Greenwich 新特性和F升级分享

    2019.01.23 期待已久的Spring Cloud Greenwich 发布了release版本,作为我们团队也第一时间把RC版本替换为release,以下为总结,希望对你使用Spring Cl ...

  5. 【JavaScript】BOM

    一.前言        接着前一章的内容,继续Js的学习. 二.内容 window对象 //确定窗口位置 var leftPos = (typeof window.screenLeft == &quo ...

  6. 洛谷 P2505 [HAOI2012]道路 解题报告

    P2505 [HAOI2012]道路 题目描述 C国有n座城市,城市之间通过m条单向道路连接.一条路径被称为最短路,当且仅当不存在从它的起点到终点的另外一条路径总长度比它小.两条最短路不同,当且仅当它 ...

  7. go日期时间函数+常用内建函数+错误处理

    日期时间函数 // 时间日期函数包 import "time" // 1. 当前时间 time.Now()-->time.Time类型 // 2. now:=time.Now ...

  8. ORACLE获取某个时间段之间的月份列表

    返回1-31,或者1-12,或者某个 select rownum   from dual   connect by rownum<31 就是connect by http://marcospri ...

  9. shell中的引用

    By francis_hao    Mar 31,2018   引用,用来移除某个字符或单词对于shell的特殊含义 每个元字符对于shell都有特殊含义,可分割单词,如果想使用其本身的含义就需要用到 ...

  10. Codeforces Round #301 (Div. 2)A B C D 水 模拟 bfs 概率dp

    A. Combination Lock time limit per test 2 seconds memory limit per test 256 megabytes input standard ...