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下yum安装jdk1.8(rpm包)和tomcat-8.5
Java是目前可移植性较高的语言,相当火热,tomcat运行就需要Java语言环境 ========= 完美的分割线 ========= 0.java简介 1)tomcat运行需要对应的Java环境, ...
- ZOJ - 3216:Compositions (DP&矩阵乘法&快速幂)
We consider problems concerning the number of ways in which a number can be written as a sum. If the ...
- 20155338 2016-2017-2 《Java程序设计》第8周学习总结
20155338 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 一.日志 API简介 java++.util.logging包提供了日志功能相关类与接口,不 ...
- PHP CURL HTTPS内存泄露问题
还原场景:通过一直运行脚本,向微信用户发送模板消息,发现运行了一段时间,内存就爆了,然后立马看了一下代码,发现跟其他的消息逻辑一模一样,唯一不一样的就是请求了微信的接口:然后继续开始找问题,发现当时使 ...
- JavaScript原型继承工作原理
原型继承的定义 当你阅读关于JS原型继承的解释时,你时常会看到以下这段文字: 当查找一个对象的属性时,JavaScript 会向上遍历原型链,直到找到给定名称的属性为止.——出自JavaScript秘 ...
- nuclio kubernetes 部署
一张参考架构图: 从图中可以看到nuclio可以运行到docker 以及kubernetes中 提供了kubernetes 部署的脚本 安装 创建命名空间 kubectl create namespa ...
- PHP安装sqlsrv和memcache扩展步骤
做了两天的实验才终于摸清楚如何将PHP连接上sqlserver数据库,以及怎样通过修改virtualhost文件来重定向,因为走得弯路比较多所以很想分享一下这次的心路历程. 第一步:安装wamp等类似 ...
- LOADRUNNER之汉字编码转换及\X00问题
我们在使用loadrunner做性能测试的时候经常会出现一些URL编码问题,如当参数中存在中文的时候 "Name=user", "Value=孟林", ENDI ...
- 【转】每天一个linux命令(16):which命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/08/2759805.html 我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面 ...
- 使用过的bug跟踪系统
MantisBT jira