Python模块之: ConfigParser 配置文件读取

http://zhangjunhd.blog.51cto.com/113473/348863 

http://docs.python.org/library/configparser.html

http://www.linux-field.com/?p=437

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进行设置
         需要调用write将内容写入配置文件。
3.例子
test.conf
[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
parse_test_conf.py
import ConfigParser
cf = ConfigParser.ConfigParser()
#read config
cf.read("test.conf")
# return all section
secs = cf.sections()
print 'sections:', secs
opts = cf.options("sec_a")
print 'options:', opts
kvs = cf.items("sec_a")
print 'sec_a:', kvs
#read by type
str_val = cf.get("sec_a""a_key1")
int_val = cf.getint("sec_a""a_key2")
print "value for sec_a's a_key1:", str_val
print "value for sec_a's a_key2:", int_val
#write config
#update value
cf.set("sec_b""b_key3""new-$r")
#set a new value
cf.set("sec_b""b_newkey""new-value")
#create a new section
cf.add_section('a_new_section')
cf.set('a_new_section''new_key''new_value')
#write back to configure file
cf.write(open("test.conf""w"))
print
sections: ['sec_b', 'sec_a']
options: ['a_key1', 'a_key2']
sec_a: [('a_key1', "i'm value"), ('a_key2', '22')]
value for sec_a's a_key1: i'm value
value for sec_a's a_key2: 22
更新后的test.conf
[sec_b]
b_newkey = new-value
b_key4 = 127.0.0.1
b_key1 = 121
b_key2 = b_value2
b_key3 = new-$r
[sec_a]
a_key1 = i'm value
a_key2 = 22
[a_new_section]
new_key = new_value
4.可能抛出的异常
exception NoSectionError
Exception raised when a specified section is not found.
exception DuplicateSectionError
Exception raised if add_section() is called with the name of a section that is already present.
exception NoOptionError
Exception raised when a specified option is not found in the specified section.
exception InterpolationError
Base class for exceptions raised when problems occur performing string interpolation.
exception InterpolationDepthError
Exception raised when string interpolation cannot be completed because the number of iterations exceeds MAX_INTERPOLATION_DEPTH. Subclass of InterpolationError.
exception InterpolationMissingOptionError
Exception raised when an option referenced from a value does not exist. Subclass of InterpolationError. New in version 2.3.
exception InterpolationSyntaxError
Exception raised when the source text into which substitutions are made does not conform to the required syntax. Subclass of InterpolationError. New in version 2.3.
exception MissingSectionHeaderError
Exception raised when attempting to parse a file which has no section headers.
exception ParsingError
Exception raised when errors occur attempting to parse a file.

转载于:https://blog.51cto.com/wiigood/1008063

python config相关推荐

  1. python config配置文件的读写--configparser

    文章目录 1 基础知识 2 读取配置文件 3 改写配置文件 3.1 删除section 3.2 字符串大小写问题 4 实例 4.1 1维两层的数据 4.2 多维两层的数据 4.3 使用am运行得到的文 ...

  2. python config模块_用Python内置模块处理ini配置文件

    原标题:用Python内置模块处理ini配置文件 简介 开发人员每天都在处理一些大型而复杂的项目, 而配置文件会帮到我们并节省不少时间.在处理配置文件过程中,无需更改源代码本身,只需要调整配置文件即可 ...

  3. python config文件的读写

    1.设置配置文件 [mysql] host = 1234 port = 3306 user = root password = Zhsy08241128 database = leartd 2.读取配 ...

  4. python batch_size_Python config.batch_size方法代码示例

    本文整理汇总了Python中config.batch_size方法的典型用法代码示例.如果您正苦于以下问题:Python config.batch_size方法的具体用法?Python config. ...

  5. python config_python config的用法

    简介 ConfigParser模块在python3中修改为configparser.这个模块定义了一个ConfigParser类,该类的作用是使用配置文件生效,配置文件的格式和windows的INI文 ...

  6. python里config_Python config.get_config方法代码示例

    本文整理汇总了Python中config.get_config方法的典型用法代码示例.如果您正苦于以下问题:Python config.get_config方法的具体用法?Python config. ...

  7. python中config命令_Python config.config方法代码示例

    本文整理汇总了Python中config.config方法的典型用法代码示例.如果您正苦于以下问题:Python config.config方法的具体用法?Python config.config怎么 ...

  8. python怎么读文件里的指定几行-Python从文件中读取指定的行以及在文件指定位置写入...

    Python从文件中读取指定的行 如果想根据给出的行号, 从文本文件中读取一行数据, Python标准库linecache模块非常适合这个任务: 测试文件内容 :This is line 1. Thi ...

  9. python 解析 配置文件

    资料: https://docs.python.org/3/library/configparser.html 环境 python 3.4.4 RawConfigParser方式 example.cf ...

最新文章

  1. 分享一个自用的,随便找到的组件,留作记录
  2. webpack 从 0 到 1 构建 vue
  3. ubuntu 16.4 安装postgreSQL,使C++链接到数据库
  4. python输入float_python – 在tensorflow中创建一个float64变量
  5. ios两个app之间传值和跳转实现(转发)
  6. 数据库的基本概念(三大范式,数据)
  7. 面经 | NLP算法岗(百度)
  8. Golang包管理工具glide简介
  9. 疑难杂症篇(一)--安装Visio与已安装的office冲突的解决方案
  10. 基于java(springboot)餐厅点餐系统源码成品(java毕业设计)
  11. 葵花卫星数据介绍与下载教程
  12. 0x00000040指定的网络名不再可用怎么办?
  13. 深入理解Flash的沙箱 – Application Domains
  14. 为什么要面向对象编程?
  15. 常州大学计算机课程表,常州大学公课表
  16. 销售 小姐姐 给买家打分系统,用 Python Django 又整了一个花活
  17. 积分球辐射光源照度均匀性
  18. 获得网易云音乐歌曲播放的url
  19. 冬季黄山(2~3 日游)攻略
  20. BP神经网络基本介绍

热门文章

  1. git 推送本地分支到远程分支 git push origin
  2. 4 计算机系统的异步性,计算机操作系统的最基本特征是什么
  3. 将下图的nfa确定化为dfa_作业8 非确定的自动机NFA确定化为DFA
  4. 利用openCV中的cvCanny函数检测人脸的边缘
  5. 【数学和算法】初识卡尔曼滤波器(三)
  6. 【深度学习】sigmoid - 二次代价函数 - 交叉熵 - logistic回归 - softmax
  7. Deep Learning for Computer Vision with MATLAB and cuDNN
  8. Java对象初始化顺序
  9. java list循环中删除元素的坑
  10. Java - 正则表达式的运用(Pattern模式和Matcher匹配)