Python 之ConfigParser
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。
中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
二、ConfigParser 初始工作
使用ConfigParser 首选需要初始化实例,并读取配置文件:
三、ConfigParser 常用方法
1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:
将输出(以下将均以简介中配置文件为例):
2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
将输出:
3. 获取指定section 的配置信息。
将输出:
4. 按照类型读取指定section 的option 信息。
同样的还有getfloat、getboolean。
将输出:
5. 设置某个option 的值。(记得最后要写回)
6.添加一个section。(同样要写回)
7. 移除section 或者option 。(只要进行了修改就要写回的哦)
点击(此处)折叠或打开
- #!/usr/bin/env python
- from ConfigParser import ConfigParser
- CONFIGFILE="f.txt"
- config=ConfigParser()
- config.read(CONFIGFILE)
- print config.get('messages','greeting')
- radius=input(config.get('messages','questions')+' ')
- print config.get('messages','result')
- print config.getfloat('numbers','pi')*radius**2
- s=config.sections()
- print'section: ',s
- o=config.options('messages')
- print'messages option: ',o
- v=config.items("messages")
- print'message de xinxi: ',v
- config.add_section('liuyang1')
- config.set('liuyang1','int','15')
- config.set('liuyang'1,'hhhh','hello world')
- config.write(open("f.txt","w"))
- print config.get('liuyang1','int')
- print config.get('liuyang1','hhhh')
- #!/usr/bin/env python
- import ConfigParser
- import sys
- config=ConfigParser.ConfigParser()
- config.add_section("book1")
- config.set("book1","title","hello world")
- config.set("book1","aut","log")
- config.write(open("f.txt","w"))
Python 之ConfigParser相关推荐
- python使用configparser读取ini格式的配置文件
python使用configparser读取ini格式的配置文件 来自Python标准库的configparser模块定义了读取和写入Microsoft Windows操作系统使用的配置文件的功能.这 ...
- python中configparser详解_Python中的ConfigParser模块使用详解
1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- 【Python的configparser模块读取.ini文件内容并输出】
configparser是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已更名小写.下文通过使用python ...
- python中configparser详解_python ConfigParser模块详解
功能介绍 在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单 ...
- Python模块: ConfigParser
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...
- Python基础-ConfigParser模块
此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见配置文件格式如下 [DEFAULT] ServerAliveI ...
- Python之configparser模块详解和使用
1 configparser安装 pip3 install configparser 2 configparser简介 用来读取配置文件的python包: 一般做自动化测试的时候,会使用到这个模块,用 ...
- python【Configparser解析配置文件库】Python3中的configparser模块
文章目录 configparser模块简介 看一下configparser生成的配置文件的格式 现在看一下类似上方的配置文件是如何生成的 读文件内容 修改 configparser模块简介 该模块适用 ...
最新文章
- Ant Design 入门-参照官方文档使用组件
- 论文笔记 Medical Entity Linking using Triplet Network
- Using Graphviz dot for ERDs, network diagrams and more
- 文科生都能看懂的机器学习教程:梯度下降、线性回归、逻辑回归
- python应用实战案例:python如何实现异步爬虫?
- [Issue Fixed]-fatal: unable to access xxx: server certificate verification
- Linux下生成动态链接库是否必须使用 -fPIC 的问题
- 使用Json.net进行序列化时,如何更改属性名称?
- 计算机读法综艺中文翻译英语,汉语综艺节目英译字幕组现状初探
- 一个500人天的BI项目实施记录
- Python 模块—计算三角形的斜边长
- day16-正则表达式
- CSS颜色搭配(超级赞的几个网站)
- 常见的网络营销方式有哪些呢?
- Jekins配置Publish over SSH详解
- JavaScript打造很酷的图片放大效果实例代码
- Linux的常见操作系统
- 如何在小程序的wxml中书写函数逻辑,wxs的使用
- 推荐几个无需注册免费的PPT模板下载网站
- 微信气泡主题设置_米老鼠微信主题怎么设置? 米老鼠微信主题气泡设置教程来啦!...
热门文章
- java spring ioc 实例_Spring 源码阅读(IOC容器)-bean的实例化以及注入
- OCR应用(证件识别、文档识别)
- 计算机考试网站配置参数错误,电脑一直显示数据配置错误怎么办
- aux ps 和top_关于vmstat,top,ps aux查看的cpu占用率不一致的问题
- Linux系统的常见命令记忆【Ubuntu】
- html 粒子漩涡特效代码(实例)
- 视听融合综述(三)Audiovisual Fusion: Challenges and New Approaches
- 天涯明月刀7月5号服务器维护,7月5日服务器临时维护更新公告(已完成)
- 程序员为何很难与他人沟通
- MySQL 8.0.13安装教程(windows 64位)