配置文件操作模块,configparser
configparser
configparser用于处理特定格式的文件,其本质上是利用open来操作文件。
# 注释1
; 注释2 [section1] # 节点
k1 = v1 # 值
k2:v2 # 值 [section2] # 节点
k1 = v1 # 值
指定格式
1、获取所有节点
|
1
2
3
4
5
6
|
import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')ret = config.sections()print(ret) |
2、获取指定节点下所有的键值对
|
1
2
3
4
5
6
|
import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')ret = config.items('section1')print(ret) |
3、获取指定节点下所有的建
|
1
2
3
4
5
6
|
import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')ret = config.options('section1')print(ret) |
4、获取指定节点下指定key的值
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')v = config.get('section1', 'k1')# v = config.getint('section1', 'k1')# v = config.getfloat('section1', 'k1')# v = config.getboolean('section1', 'k1')print(v) |
5、检查、删除、添加节点
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')# 检查has_sec = config.has_section('section1')print(has_sec)# 添加节点config.add_section("SEC_1")config.write(open('xxxooo', 'w'))# 删除节点config.remove_section("SEC_1")config.write(open('xxxooo', 'w')) |
6、检查、删除、设置指定组内的键值对
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import configparserconfig = configparser.ConfigParser()config.read('xxxooo', encoding='utf-8')# 检查has_opt = config.has_option('section1', 'k1')print(has_opt)# 删除config.remove_option('section1', 'k1')config.write(open('xxxooo', 'w'))# 设置config.set('section1', 'k10', "123")config.write(open('xxxooo', 'w')) |
配置文件操作模块,configparser的更多相关文章
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- Python中配置文件解析模块-ConfigParser
Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置).配置文件的格式是: []包含的叫section, section 下有option=value ...
- python 配置文件解析模块 configparser
import ConfigParser //实例化cf = ConfigPraser.ConfigPraser()cf.read("配置文件") //获取所有sections.也就 ...
- python基础——15(加密、excel操作、ini文件操作、xml操作模块及数据格式分类)
一.加密模块 1.有解密的加密方式(base64) #base64加密 import base64 str_encrypt = input("输入要加密的字符串:\n") base ...
- Python之配置文件模块 ConfigParser
写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...
- Python模块:配置文件解析器configparser
版权声明:本文为博主皮皮http://blog.csdn.net/pipisorry原创文章,未经博主同意不得转载. https://blog.csdn.net/pipisorry/article/d ...
- hashlib模块configparser模块logging模块
hashlib模块 算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长 ...
- os模块,os.path模块,subprocess模块,configparser模块,shutil模块
1.os模块 os表示操作系统该模块主要用来处理与操作系统相关的操作最常用的文件操作打开 读入 写入 删除 复制 重命名 os.getcwd() 获取当前执行文件所在的文件夹路径os.chdir(&q ...
- python day 9: xlm模块,configparser模块,shutil模块,subprocess模块,logging模块,迭代器与生成器,反射
目录 python day 9 1. xml模块 1.1 初识xml 1.2 遍历xml文档的指定节点 1.3 通过python手工创建xml文档 1.4 创建节点的两种方式 1.5 总结 2. co ...
随机推荐
- servlet监听器Listener(理论+例子)
Listener采用了观察者模式(24种模式之一),Listener是servlet的监听器,他可以监听客户端的请求.服务器端的操作等, 通过监听器,可以自动激发一些操作.比如:监听在线用户数量 当增 ...
- 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)
圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...
- C++编译期多态与运行期多态
前言 今日的C++不再是个单纯的"带类的C"语言,它已经发展成为一个多种次语言所组成的语言集合,其中泛型编程与基于它的STL是C++发展中最为出彩的那部分.在面向对象C++编程中, ...
- epoch和Iteration
做机器学习时遇到epoch和iteration,一开始有点迷惑.不是一个意思吗? epoch可以翻译成"回合".一个epoch内,做一次train+一次test iteration ...
- Linux中/etc/hosts文件总是被自动修改
关闭NetworkManager服务即可. 临时关闭: service NetworkManager stop 永久关闭: chkconfig NetworkManager off 在centos6 ...
- Jenkins的Publish Over FTP Plugin插件参数使用
无论在Windows还是Linux下,都是采用这样方式${WORKSPACE}
- Birthday-24
2013 LEXUS花样滑冰 和母亲在一起,生日快乐!
- FZU 2184 逆序数还原
传送门 Description 有一段时间Eric对逆序数充满了兴趣,于是他开始求解许多数列的逆序数(对于由1...n构成的一种排列数组a,逆序数即为满足i<j,ai>aj的数字对数),但 ...
- HDU 4768 Flyer(二分)
题目链接: 传送门 Flyer Time Limit: 1000MS Memory Limit: 32768 K Description The new semester begins! Di ...
- JS中的"=="转换规则
number类型与string类型比较,string会转换为number类型 '' == '0' //false 0 == ''//true; 0 == '0'//true ' \t\r\n '==0 ...