一、ConfigParser简介

ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。

1: [db] 2: db_host = 127.0.0.1 3: db_port = 22 4: db_user = root 5: db_pass = rootroot 6:  7: [concurrent] 8: thread = 10 9: processor = 20

中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。

二、ConfigParser 初始工作

使用ConfigParser 首选需要初始化实例,并读取配置文件:

1: cf = ConfigParser.ConfigParser() 2: cf.read( "配置文件名")

三、ConfigParser 常用方法

1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:

1: s = cf.sections() 2: print 'section:', s

将输出(以下将均以简介中配置文件为例):

1: section: ['db', 'concurrent']

2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:

1: o = cf.options( "db") 2: print 'options:', o

将输出:

1: options: ['db_host', 'db_port', 'db_user', 'db_pass']

3. 获取指定section 的配置信息

1: v = cf.items( "db") 2: print 'db:', v

将输出:

1: db: [('db_host', '127.0.0.1'), ('db_port', '22'), ('db_user', 'root'), ('db_pass', 'rootroot')]

4. 按照类型读取指定section 的option 信息

同样的还有getfloat、getboolean。

1: #可以按照类型读取出来 2: db_host = cf.get( "db", "db_host") 3: db_port = cf.getint( "db", "db_port") 4: db_user = cf.get( "db", "db_user") 5: db_pass = cf.get( "db", "db_pass") 6:  7: # 返回的是整型的 8: threads = cf.getint( "concurrent", "thread") 9: processors = cf.getint( "concurrent", "processor") 10:  11: print "db_host:", db_host 12: print "db_port:", db_port 13: print "db_user:", db_user 14: print "db_pass:", db_pass 15: print "thread:", threads 16: print "processor:", processors

将输出:

1: db_host: 127.0.0.1 2: db_port: 22 3: db_user: root 4: db_pass: rootroot 5: thread: 10 6: processor: 20

5. 设置某个option 的值。(记得最后要写回)

1: cf.set( "db", "db_pass", "zhaowei") 2: cf.write(open( "test.conf", "w"))

6.添加一个section。(同样要写回)

1: cf.add_section('liuqing') 2: cf.set('liuqing', 'int', '15') 3: cf.set('liuqing', 'bool', ' true') 4: cf.set('liuqing', 'float', '3.1415') 5: cf.set('liuqing', 'baz', 'fun') 6: cf.set('liuqing', 'bar', 'Python') 7: cf.set('liuqing', 'foo', '%(bar)s is %(baz)s!') 8: cf.write(open( "test.conf", "w"))

7. 移除section 或者option 。(只要进行了修改就要写回的哦)

1: cf.remove_option('liuqing','int') 2: cf.remove_section('liuqing') 3: cf.write(open( "test.conf", "w"))

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. from ConfigParser import ConfigParser
  3. CONFIGFILE="f.txt"
  4. config=ConfigParser()
  5. config.read(CONFIGFILE)
  6. print config.get('messages','greeting')
  7. radius=input(config.get('messages','questions')+' ')
  8. print config.get('messages','result')
  9. print config.getfloat('numbers','pi')*radius**2
  10. s=config.sections()
  11. print'section: ',s
  12. o=config.options('messages')
  13. print'messages option: ',o
  14. v=config.items("messages")
  15. print'message de xinxi: ',v
  16. config.add_section('liuyang1')
  17. config.set('liuyang1','int','15')
  18. config.set('liuyang'1,'hhhh','hello world')
  19. config.write(open("f.txt","w"))
  20. print config.get('liuyang1','int')
  21. print config.get('liuyang1','hhhh')
  22. #!/usr/bin/env python
  23. import ConfigParser
  24. import sys
  25. config=ConfigParser.ConfigParser()
  26. config.add_section("book1")
  27. config.set("book1","title","hello world")
  28. config.set("book1","aut","log")
  29. config.write(open("f.txt","w"))

Python 之ConfigParser相关推荐

  1. python使用configparser读取ini格式的配置文件

    python使用configparser读取ini格式的配置文件 来自Python标准库的configparser模块定义了读取和写入Microsoft Windows操作系统使用的配置文件的功能.这 ...

  2. python中configparser详解_Python中的ConfigParser模块使用详解

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...

  3. python中confIgparser模块学习

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

  4. 【Python的configparser模块读取.ini文件内容并输出】

    configparser是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已更名小写.下文通过使用python ...

  5. python中configparser详解_python ConfigParser模块详解

    功能介绍 在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单 ...

  6. Python模块: ConfigParser

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...

  7. Python基础-ConfigParser模块

    此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见配置文件格式如下 [DEFAULT] ServerAliveI ...

  8. Python之configparser模块详解和使用

    1 configparser安装 pip3 install configparser 2 configparser简介 用来读取配置文件的python包: 一般做自动化测试的时候,会使用到这个模块,用 ...

  9. python【Configparser解析配置文件库】Python3中的configparser模块

    文章目录 configparser模块简介 看一下configparser生成的配置文件的格式 现在看一下类似上方的配置文件是如何生成的 读文件内容 修改 configparser模块简介 该模块适用 ...

最新文章

  1. Ant Design 入门-参照官方文档使用组件
  2. 论文笔记 Medical Entity Linking using Triplet Network
  3. Using Graphviz dot for ERDs, network diagrams and more
  4. 文科生都能看懂的机器学习教程:梯度下降、线性回归、逻辑回归
  5. python应用实战案例:python如何实现异步爬虫?
  6. [Issue Fixed]-fatal: unable to access xxx: server certificate verification
  7. Linux下生成动态链接库是否必须使用 -fPIC 的问题
  8. 使用Json.net进行序列化时,如何更改属性名称?
  9. 计算机读法综艺中文翻译英语,汉语综艺节目英译字幕组现状初探
  10. 一个500人天的BI项目实施记录
  11. Python 模块—计算三角形的斜边长
  12. day16-正则表达式
  13. CSS颜色搭配(超级赞的几个网站)
  14. 常见的网络营销方式有哪些呢?
  15. Jekins配置Publish over SSH详解
  16. JavaScript打造很酷的图片放大效果实例代码
  17. Linux的常见操作系统
  18. 如何在小程序的wxml中书写函数逻辑,wxs的使用
  19. 推荐几个无需注册免费的PPT模板下载网站
  20. 微信气泡主题设置_米老鼠微信主题怎么设置? 米老鼠微信主题气泡设置教程来啦!...

热门文章

  1. java spring ioc 实例_Spring 源码阅读(IOC容器)-bean的实例化以及注入
  2. OCR应用(证件识别、文档识别)
  3. 计算机考试网站配置参数错误,电脑一直显示数据配置错误怎么办
  4. aux ps 和top_关于vmstat,top,ps aux查看的cpu占用率不一致的问题
  5. Linux系统的常见命令记忆【Ubuntu】
  6. html 粒子漩涡特效代码(实例)
  7. 视听融合综述(三)Audiovisual Fusion: Challenges and New Approaches
  8. 天涯明月刀7月5号服务器维护,7月5日服务器临时维护更新公告(已完成)
  9. 程序员为何很难与他人沟通
  10. MySQL 8.0.13安装教程(windows 64位)