python模块之configparser
https://docs.python.org/3/library/configparser.html
configparser用于处理特定格式的文件,其本质上是利用open来操作文件。
# 注释1
; 注释2 [section1] # 节点
k1 = v1 # 值
k2:v2 # 值 [section2] # 节点
k1 = v1 # 值
规定格式
1、获取所有节点
import configparser config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.sections()
print(ret)
2、获取指定节点下所有的键值对
import configparser config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.items('section1')
print(ret)
3、获取指定节点下所有的建
import configparser config = configparser.ConfigParser()
config.read('xxxooo', encoding='utf-8')
ret = config.options('section1')
print(ret)
4、获取指定节点下指定key的值
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、检查、删除、添加节点
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、检查、删除、设置指定组内的键值对
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', "")
config.write(open('xxxooo', 'w'))
python模块之configparser的更多相关文章
- Python模块之: ConfigParser 配置文件读取
Python模块之: ConfigParser 配置文件读取 ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...
- python 模块之-configparser
python 模块configparser 配置文件模块 import configparser config = configparser.ConfigParser() config[&q ...
- Python模块学习 - ConfigParser
配置文件 很多软件都用到了配置文件,像git运行的时候会读取~/gitconfig,MySQL运行的时候会读取/etc/my.cnf,Python 提供的包管理工具pip命令,也会去读取~/.pip/ ...
- Python模块之ConfigParser - 读写配置文件
Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...
- Python 模块续 configparser、shutil、XML、paramiko、系统命令、
一.configparse # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section2] # 节点 k1 = v1 # 值 1.获取所有节点 ...
- Python模块:configparser、hashlib、(subprocess)
configparser模块: 此模块用于生成和修改常见配置文档. 一个常见配置文件(.ini的后缀名)格式如下: [DEFAULT] # DEFAULT 是指后面的字典里都会默认有的内容 Serve ...
- python模块:configparser
"""Configuration file parser. A configuration file consists of sections, lead by a &q ...
- python模块之ConfigParser: 用python解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...
- Python 模块之 ConfigParser: 用 Python 解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做 ...
随机推荐
- Postgresql FATAL: could not create semaphores: No space left on device
昨天安装完成pg 9.5后,启动报错: FATAL: could not create semaphores: No space left on device DETAIL: Failed sys ...
- visual studio code
偏好设置: "editor.fontSize": 26, "editor.tabSize": 8, "editor.mouseWheelZoom&qu ...
- python 模块库
python mod --------------------os https://segmentfault.com/a/1190000003061079logging ...
- Rails : 产品环境(生产环境)的部署
bundle install rails server (默认为开发环境) rails server -p80 -e production (指定为生产环境 ,并自定义指定站点端口) rake RAI ...
- 延时函数出错,volatile一例
莫名其妙的错误.使用Systick做的延时. 初始化是这样的: //SysTick配置 SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); if ...
- 安卓 JDK、SDK、ADT 区别
问题一:android软件开发是用java语法,但是为什么开发环境还需要jdk,有android sdk不就可以了吗? 答: 我知道写字要用笔,但为什么还需要笔芯(墨水),有笔杆不就可以了吗? 问题二 ...
- mysql数据库中如何修改已建好的表中的【列名】【列的属性】
sql命令:alter table tbl_name change old_col_name new_col_name data_type not null auto_increment primar ...
- boost multi_index
/** boost 多索引容器的一般使用 这里使用google 的gmock 库来验证对boost 多索引模板的使用,并验证. 这里是手敲的,可能会有一些字符敲错的情况,编译错误的放,修改一下,同时链 ...
- DBSCAN——python实现
# -*- coding: utf-8 -*- from matplotlib.pyplot import * from collections import defaultdict import r ...
- 坦克大战,看你能坚持几秒 ~~Duang~~Duang
闲来无事,写了一个坦克大战的小游戏,打开页面就能看到源码,代码还没有来得及整理.大家闲来玩玩吧,看谁玩的时间长! http://xiaohaibaomu.com/home/index