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环境, ...
- ubuntu16.04 下 NVIDIA GTX1050ti 显卡驱动 PPA安装
本文参考资料链接: http://blog.csdn.net/10km/article/details/61191230 前几天在京东商城上花了6999元买了台笔记本(惠普(HP)暗影精灵II代Pro ...
- EasyTouch中多种QuickGesture手势检测
EasyTouch中QuickGesture的用法 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Qu ...
- hive中实现类似MySQL中的group_concat功能
hive> desc t; OK id string str string Time taken: 0.249 seconds hive> select * from t ...
- 【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.2 ruleflow-group&salience
转载至:https://blog.csdn.net/wo541075754/article/details/75299888 ruleflow-group 在使用规则流的时候要用到ruleflow-g ...
- ory Oathkeeper docker-compose 安装运行
Oathkeeper 相关介绍可以参考官方文档,主要就是cloud native 身份以及访问代理 运行环境使用docker 安装(api proxy ) Dockerfile api: docker ...
- golang 自定义importpath
golang 的包导入和其他语言有好多不一样的地方,以下是一个自定义的导入 golang 自定义导入说明 一个官方的说明 比较简单,就不翻译了,主要是说我们可以通过添加meta 数据告诉包如何进行加载 ...
- 一个简单的批量更新oracle 数据库中 最近的服务商名称的数据
有一个需求是这样的,我们需要更新数据库中的数据,数据时这样的 1.大约50万以上 2. 数据中有较多的重复数据 3. 需要将表中最近的代理商的名称赋值给行中的服务商名称 4. 代理商的名称可能有多个, ...
- 使用cglib动态创建javabean
动态创建javabean对于我们进行开发,会有一定的帮助,一下是使用cglib动态创建javabean的一个例子: 先贴上code: package com.dalong.CreateCode; im ...
- 应用解决告诉你什么时候该用ajax
第一.请求的提交是为了页面数据的显示,这时候用户一般不希望看到页面的刷新,是使用AJAX的一个最佳时候. 第二.如果请求提交后,用户能从页面感觉到提交结果,这时候,也最好不要有页面刷新,推荐使用AJA ...