python之ConfigParser
以前傻傻的不知道还有configParser这么方便的模块,都是一个个的解析转换……
配置文件xxxxx
# 注释1
; 注释2
[section1] # 节点
k1 = v1 # 值
k2:v2 # 值
[section2] # 节点
k1 = v1 # 值
k2=['123','456']
节点必须是用[],节点下面的信息必须使用键值对
使用#和;都可以注释信息
1、获取所有节点
import configparser
config = configparser.ConfigParser()
config.read(‘xxxxx’, encoding='utf-8')
ret = config.sections()
print ret
2、获取指定节点下所有的键值对
import configparser
config = configparser.ConfigParser()
config.read(‘xxxxx’, encoding='utf-8')
ret = config.items('section1')
print ret
3、获取指定节点下所有的建
import configparser
config = configparser.ConfigParser()
config.read(‘xxxxx’, encoding='utf-8')
ret = config.options('section1')
print ret
4、获取指定节点下指定key的值
import configparser
config = configparser.ConfigParser()
config.read(‘xxxxx’, encoding='utf-8')
v = config.get('section1', 'k1')
5、检查、删除、添加节点
import configparser
config = configparser.ConfigParser()
config.read(‘xxxxx’, encoding='utf-8')
# 检查
has_sec = config.has_section('section1')
print has_sec
# 添加节点(只要进行了修改,就必须回写,不然信息不保存)
config.add_section("SEC_1")
config.write(open(‘xxxxx’, 'w'))
#文件信息被写之后,注释信息自动消失
#删除section或者option
config.remove_section("SEC_1")
config.write(open(‘xxxxx’, 'w'))
python之ConfigParser的更多相关文章
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- python之ConfigParser的使用。
一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- python 的ConfigParser模块
Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...
- 记一次用python 的ConfigParser读取配置文件编码报错
记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
- python 之ConfigParser
ConfigParser 简介ConfigParser是用来操作配置文件的模块. 说明:[**]为配置文件的section,基本格式为 [section] key = valueeg: [db] db ...
- Python利用ConfigParser读取配置文件
http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...
随机推荐
- 【linux基础】ubuntu如何查看linux的内核版本和系统版本
参考 1.查看linux内核和系统版本: 完
- Shader的函数公式以及使用的场景
Shader 是干什么的? 它的作用可以先简单理解为对屏幕上的物体,进行颜色处理. 而大家都知道,所有计算机,处理颜色的当然是显卡,也就是 GPU . 所以我们写 Shader 的目的就是告诉 GPU ...
- 2018-2019-2 20165212 《网络对抗技术》Exp3 免杀原理与实践
2018-2019-2 20165212 <网络对抗技术>Exp3 免杀原理与实践 一.实验内容 正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion ...
- nyoj 吃土豆
吃土豆 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Bean-eating is an interesting game, everyone owns an M* ...
- spring cloud 之 Eureka 知识点
Eureka原理 当服务消费者想要调用服务提供者的API时,首先会在注册中心中查询当前可用的实例的网络地址(也可能是定时查询可用实例,本地缓存好可用服务列表),然后再使用客户端负载均衡,命中到其中一个 ...
- JUC集合之 LinkedBlockingDeque
LinkedBlockingDeque介绍 LinkedBlockingDeque是双向链表实现的双向并发阻塞队列.该阻塞队列同时支持FIFO和FILO两种操作方式,即可以从队列的头和尾同时操作(插入 ...
- mysql常用语法操作
一.用户管理: 1.新建用户: >CREATE USER name IDENTIFIED BY 'ssapdrow'; 2.更改密码: >SET PASSWORD FOR name=PAS ...
- FineUI中在一个页面中通过控件事件(JS)向父页面中添加Tab页
1.在前台页面尾部添加js代码 </form> <script type="text/javascript"> var basePath ...
- phper必知必会之类库自动加载的七种方式(三)
## php自动加载 下面显示例子的文件目录结构图 一.没有使用命名空间的几种实现 test/oneClass.php class oneClass{ public function show(){ ...
- 终于完成了 源码 编译lnmp环境
经过了大概一个星期的努力,终于按照海生的编译流程将lnmp环境源码安装出来了 nginx 和php 主要参考 http://hessian.cn/p/1273.html mysql 主要参考 http ...