标准库ConfigParser模块
用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。
来看一个好多软件的常见文档格式如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
[DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no |
如果想用python生成一个这样的文档怎么做呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import configparser #in Python 2.x ConfigParser |
写完了还可以再读出来哈。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
>>> import configparser >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read( 'example.ini' ) [ 'example.ini' ] >>> config.sections() [ 'bitbucket.org' , 'topsecret.server.com' ] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config[ 'bitbucket.org' ][ 'User' ] 'hg' >>> config[ 'DEFAULT' ][ 'Compression' ] 'yes' >>> topsecret = config[ 'topsecret.server.com' ] >>> topsecret[ 'ForwardX11' ] 'no' >>> topsecret[ 'Host Port' ] '50022' >>> for key in config[ 'bitbucket.org' ]: print (key) ... user compressionlevel serveraliveinterval compression forwardx11 >>> config[ 'bitbucket.org' ][ 'ForwardX11' ] 'yes' |
configparser增删改查语法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
[section1] k1 = v1 k2:v2 [section2] k1 = v1 #以上为配置文件显示的结构
import ConfigParser config = ConfigParser.ConfigParser() config.read( 'i.cfg' ) # ########## 读 ########## #secs = config.sections() #print(secs) #options = config.options('group2') #print(options) #item_list = config.items('group2') #print(item_list) #val = config.get('group1','key') #val = config.getint('group1','key') # ########## 改写 ########## #sec = config.remove_section('group1') #config.write(open('i.cfg', "w")) #sec = config.has_section('wupeiqi') #sec = config.add_section('wupeiqi') #config.write(open('i.cfg', "w")) #config.set('group2','k1',11111) #config.write(open('i.cfg', "w")) #config.remove_option('group2','age') #config.write(open('i.cfg', "w")) |
标准库ConfigParser模块的更多相关文章
- Python 标准库 ConfigParser 模块 的使用
Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...
- [python标准库]Pickle模块
Pickle-------python对象序列化 本文主要阐述以下几点: 1.pickle模块简介 2.pickle模块提供的方法 3.注意事项 4.实例解析 1.pickle模块简介 The pic ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- [python标准库]XML模块
1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...
- 【python】Python标准库defaultdict模块
来源:http://www.ynpxrz.com/n1031711c2023.aspx Python标准库中collections对集合类型的数据结构进行了很多拓展操作,这些操作在我们使用集合的时候会 ...
- python标准库 bisect模块
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #bisect #作用:维护有序列表,而不必在每次向列表增加一个元素 ...
- python标准库 sysconfig模块
# -*- coding: utf-8 -*-# python:2.x__author__ = 'Administrator'import sysconfig#sysconfig:解释器编译时配置#作 ...
- Python标准库--os模块
这个模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Windows下运行.一个例 ...
- Python标准库 -- UUID模块(生成唯一标识)
UUID是什么: UUID: 通用唯一标识符 ( Universally Unique Identifier ),对于所有的UUID它可以保证在空间和时间上的唯一性,也称为GUID,全称为: UUID ...
随机推荐
- JMeter 接口测试 自动生成签名机制
在进行接口测试时,遇到接口进行了签名校验,为实现自动生成签名,经过一点研究终于成功. 首先,需要从前端获取 签名加密包 XXXsign.jar.. 建议将该jar包放在 jmeter lib 目录 ...
- 《MySQL实战45讲》学习笔记4——MySQL中InnoDB的索引
索引是在存储引擎层实现的,且在 MySQL 不同存储引擎中的实现也不同,本篇文章介绍的是 MySQL 的 InnoDB 的索引. 下文将以这张表为例开展. # 创建一个主键为 id 的表,表中有字段 ...
- SpringMVC中利用HandlerExceptionResolver完成异常处理
在解决Controller层中的异常问题时,如果针对每个异常处理相对较为繁琐.在SpringMVC中提供了HandlerExceptionResolver用于处理捕获到的异常,从而重新定义返回给前端的 ...
- AJ学IOS(28)UI之Quartz2D简单介绍
AJ分享,必须精品 iOS开发UI篇—Quartz2D简单介绍 什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : ...
- AJ学IOS(20)UI之UIPickerView_点菜系统
AJ分享,必须精品 先看效果图 ## UIPickerView控件 UIPickerView用处: 用来展示很多行(row) 很多列(component )的数据,多用于电子商务的点菜,城市选择等等. ...
- 【Java】关键字 和 保留字
Java的关键字[Keyword]和 保留字[Reserved word] 官方描述: https://docs.oracle.com/javase/tutorial/java/nutsandbolt ...
- 利用opencv实现视频捕捉功能
import cv2 as cv import numpy as np def video_demo(): capture = cv.VideoCapture(0) #打开摄像头,参数0代表设备ID( ...
- 技术债务(Technical debt)的产生原因及衡量解决
第一次发布代码,就好比借了一笔钱.只要通过不断重写来偿还债务,小额负债可以加速开发.但久未偿还债务会引发危险.复用马马虎虎的代码,类似于负债的利息.整个部门有可能因为松散的实现,不完全的面向对象的设计 ...
- stand up meeting 11/23/2015
组员 周末工作 工作耗时/h 明日计划 计划耗时/h 冯晓云 完成网络状况的侦听,对网络连接断开,WiFi状况不佳,未查询到结果设定不同的返回码,按约定格式返回:对于超时未查询进行了相关的学习: 3 ...
- Windows10 下安装SourceTree 跳过注册 Bitbucket
前提:安装Git 下载Windows系统可执行文件SourceTreeSetup-3.1.3.exe 双击安装,会出现如下界面: 关闭这个界面 C:\Users\当前用户文件夹\AppData\Loc ...