python 配置文件 ConfigParser模块
ConfigParser模块
用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。
来看一个好多软件的常见文档格式如下
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no
python生成配置文件
import configparser config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': ''} config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)
读取配置文件信息
#!/usr/bin/env python3
# -*- coding:utf-8 -*- import configparser config = configparser.ConfigParser()
config.sections() config.read('example') config.sections() for i in config.keys():
print(i, end=' ')
print("=" * 50)
print(config.keys()) print(config['bitbucket.org']['User']) print(config['DEFAULT']['Compression']) topsecret = config['topsecret.server.com']
print(topsecret['ForwardX11'])
print(topsecret['Port']) for key in config['bitbucket.org']:
print(key) print(config['bitbucket.org']['ForwardX11'])
configparser增删改查语法
########### i.cfg file ##########
[section1]
k1 = v1
k2:v2
k5 : 10 [section2]
k3 = v3
k4 = v4
age : 88
#################################
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# author: songwei import configparser # ########## 读 ########## config = configparser.ConfigParser()
config.read('example') secs = config.sections()
print(secs) #--->['section1', 'section2']
options = config.options('section2')
print(options) #--->['k3', 'k4', 'age'] item_list = config.items('section2')
print(item_list) #--->[('k3', 'v3'), ('k4', 'v4'), , ('age', '88')] val = config.get("section1", "k2")
print(val) #--->v2
val2 = config.getint('section1','k5')
print(val2) #---> 10 # ########## 改写 ########## config = configparser.ConfigParser()
config.read('example')
sec = config.remove_section('section1') #删除
config.write(open('i.cfg', "w")) sec = config.has_section('wupeiqi')
print(sec)
sec = config.add_section('wupeiqi')
print(sec)
config.write(open('i.cfg', "w")) config.set('section2','k4',"")
# config.defaults()
config.write(open('i.cfg', "w")) # config.remove_option('section2','age')
# config.write(open('i.cfg', "w"))
python 配置文件 ConfigParser模块的更多相关文章
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- 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模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- Python自动化测试 -ConfigParser模块读写配置文件
C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...
- Python操作配置文件configparser模块
在实际的开发过程中,我们常有操作ini格式和conf格式配置文件的操作,Python为我们提供了configparser模块,方便我们对配置文件进行读写操作. config.ini配置文件内容如下: ...
- Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件
前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...
- python中configparser模块的使用
configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...
随机推荐
- Spark GraphX 属性图操作
package Spark_GraphX import org.apache.spark.graphx._ import org.apache.spark.rdd.RDD import org.apa ...
- 配置URL
- KeepAlived的实现示例
KeepAlived的实现示例 KeepAlived的实现 HA Cluster配置准备: 各节点时间必须同步 ntp(6), chrony(7) 1>在centos6上 ntpdate 172 ...
- tyvj 2054 [Nescafé29]四叶草魔杖【克鲁斯卡尔+状压dp】
传送:http://www.joyoi.cn/problem/tyvj-2054 来自lyd课件: 所以先预处理出各个sum为0的块,然后状压dfs取min来得到答案 #include<iost ...
- [App Store Connect帮助]六、测试 Beta 版本(2)输入测试信息以供外部测试
如果您向外部测试员分发您的 App,您需要输入关于您 App 的额外 TestFlight 测试信息以供“Beta 版 App 审核”.您可以在添加 App 至您的帐户时,或在您邀请外部测试员时输入此 ...
- centos 安装sysbench
安装sysbench 下载并且解压 shell> wget https://github.com/akopytov/sysbench/archive/1.0.zip -O "sysbe ...
- Ubuntu 16.04 安装OpenSSH7.4
前几天突然收到接到网安总队下发通知说我们在aws里面的服务器存在重大漏洞及安全隐患.必须在规定时间内修改.我们收到邮件打开Excel发现这些问题 是由于OpenSSH版本太低导致的.于是便安排紧急 ...
- Traceback (most recent call last): File "setup.py", line 22, in <module> execfile(join(CURDIR, 'src', 'SSHLibrary', 'version.py')) NameError: name 'execfile' is not defined
在python3环境下安装robotframework-SSHLibraray报错: Traceback (most recent call last): File "setup.py&qu ...
- Required diagnostic data collection for RMAN backup
1. Provide the alert.log and related tracefile of the target database. 2. Provide details on the l ...
- python与arduino串口通讯对接opencv实现智能物品分拣
2018-05-0118:53:50 先上图: 再来视频: http://v.youku.com/v_show/id_XMzU3NzAwNzMyNA==.html?spm=a2hzp.8244740. ...