配置文件操作模块,configparser
configparser
configparser用于处理特定格式的文件,其本质上是利用open来操作文件。
# 注释1
; 注释2 [section1] # 节点
k1 = v1 # 值
k2:v2 # 值 [section2] # 节点
k1 = v1 # 值
指定格式
1、获取所有节点
1
2
3
4
5
6
|
import configparser config = configparser.ConfigParser() config.read( 'xxxooo' , encoding = 'utf-8' ) ret = config.sections() print (ret) |
2、获取指定节点下所有的键值对
1
2
3
4
5
6
|
import configparser config = configparser.ConfigParser() config.read( 'xxxooo' , encoding = 'utf-8' ) ret = config.items( 'section1' ) print (ret) |
3、获取指定节点下所有的建
1
2
3
4
5
6
|
import configparser config = 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 configparser config = 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 configparser config = 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 configparser config = 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 ...
随机推荐
- Selenium+WebDriver+Python 定时控制任务
为了更对得起"自动化测试"的名号,我们可以设置定时任务,使我们自动化脚本在某个时间点自动运行脚本,这样就可以让测试在夜间进行,减少了时间成本.通过程序来控制test case在什么 ...
- 翻滚吧,Spark (错误记录)
1) 本地运行报错: Exception in thread "main" org.apache.spark.SparkException: A master URL must b ...
- 65.Android 三大图片缓存原理、特性对比 (转)
这是 Trinea 在 MDCC 上分享的内容(略微改动),也是源码解析第一期发布时介绍的源码解析后续会慢慢做的事. 从总体设计和原理上对几个图片缓存进行对比,没用到他们的朋友也可以了解他们在某些特性 ...
- 高级语言虚拟机的一点理解,对比.NET和Java平台
最近学习了一些高级语言虚拟机的知识,在此对.NET平台和java平台做一个简单的比较.对java平台已经很熟了,所以此处只介绍.NET平台下的一些概念. 一.CLI 通用语言基础架构(Common L ...
- FAILED java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI:hdfs:192.*
运行的参数配置 hdfs:192.168.58.180/cf/userItem.txt 应该写成 hdfs://192.*
- bzoj1396: 识别子串
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- 关于markdown的学习
标题 标题(一个等号) 小标题 深层标题 深层标题2 深层标题6 深层标题 左边一个空格 深层标题 左边两个空格 此处省略很多字 左边有回车 一个空格 两个空格 三个空格 十个空格 字体属性:斜体.加 ...
- 【Alpha阶段】第⑨次Scrum例会
会议信息 因编译作业发布,暂时没有进展 时间:2016.11.03 21:30 时长:5min 地点:大运村1号公寓 类型:日常Scrum会议 NXT:2016.11.05 21:30 个人任务报告 ...
- Python核心编程第三版第二章学习笔记
第二章 网络编程 1.学习笔记 2.课后习题 答案是按照自己理解和查阅资料来的,不保证正确性.如由错误欢迎指出,谢谢 1. 套接字:A network socket is an endpoint of ...