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. Django 2.0 学习(18):Django 缓存、信号和extra

    Django 缓存.信号和extra Django 缓存 由于Django是动态网站,所以每次请求均会去数据库进行相应的操作,当程序访问量大时,耗时必然会显著增加.最简单的解决方法是:使用缓存,缓存将 ...

  2. MySQL join 使用方法

    JOIN 按照功能大致分为如下三类: INNER JOIN(内连接,或等值连接):取得两个表中存在连接匹配关系的记录. LEFT JOIN(左连接):取得左表(table1)完全记录,即是右表(tab ...

  3. Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)

    链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS   Memory Limit: 131072K To ...

  4. sql语句查询各门课程平均分的最大值

    解法一: select courseno,stuno,avg(score) '平均分最高值'--这里是求平均,后面的条件是过滤最大值的 from tablename group by courseno ...

  5. TCP/IP协议详解---概述

        工作之后,才发现以前在学校里学的东西忘得太快太干净了,现在需要一点点地捡起来了,要不然写几行程序会闹很多笑话会出现很多bug的.从今天开始,翻一翻<TCP/IP协议详解 卷1>这本 ...

  6. windows下载执行命令大全

      1.bitsadmin命令(只能命令下载到指定路径上,win7以上): bitsadmin /transfer myDownLoadJob /download /priority normal & ...

  7. Codeforces 576C. Points on Plane(构造)

    将点先按x轴排序,把矩形竖着划分成$10^3$个块,每个块内点按y轴排序,然后蛇形走位上去. 这样一个点到下一个点的横坐标最多跨越$10^3$,一共$10^6$个点,总共$10^9$,一个块内最多走$ ...

  8. Codeforces Round #298 (Div. 2)A B C D

    A. Exam time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  9. 手脱ASPack v2.12

    1.PEID查壳提示为: ASPack 2.12 -> Alexey Solodovnikov 2.载入OD,程序入口点是一个pushad,在他的下一行就可以进行ESP定律,下硬件访问断点然后s ...

  10. mysql5.7 安装常见问题

    mysql 5.7安装要执行以下三个命令: 1.mysql install2.mysqld --initialize3.net start mysql 第一个命令可能会出现Install/Remove ...