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 ...
随机推荐
- Cocos2d-x 3.2编译生成Android程序出错Error running command, return code: 2的解决方法
用Cocos2d-x 3.2正式版创建项目,结果使用cocos compile -p android编译生成APK程序,结果悲剧了,出现以下错误. Android NDK: Invalid APP_S ...
- bzoj 2726: [SDOI2012]任务安排【cdq+斜率优化】
cdq复健.jpg 首先列个n方递推,设sf是f的前缀和,st是t的前缀和: \[ f[i]=min(f[j]+s*(sf[n]-sf[j])+st[i]*(sf[i]-sf[j])) \] 然后移项 ...
- AppConfig 操作简易封装
using System; using System.Configuration; namespace HT.IMS.Common { public class ClientConfig { ; pu ...
- oracle 自定义类型 type / create type
一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarchar2. 2.数值类型.如:int.number(p,s).integ ...
- 233 Number of Digit One 数字1的个数
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...
- Java多线程——线程之间的同步
Java多线程——线程之间的同步 摘要:本文主要学习多线程之间是如何同步的,如何使用volatile关键字,如何使用synchronized修饰的同步代码块和同步方法解决线程安全问题. 部分内容来自以 ...
- CF540C Ice Cave
思路: 搜索. 加回溯会超时,不加回溯就过了,不是很理解. 实现: #include <iostream> #include <cstdio> using namespace ...
- 修改 进程占用资源限制ulimit(限制服务器的链接数目)
ulimit用于限制shell启动进程所占用的资源.其中ulimit -n用于限制进程能够打开的文件描述符的最大数目.因为任何设备在linux下都是文件,通信的接口也有专门的接口文件负责,所以linu ...
- Tornado引入静态css、js文件
一.静态路径 template_path=os.path.join(os.path.dirname(__file__), "templates") 这里是设置了模板的路径,放置模板 ...
- Maven对不同的测试环境用不同的参数进行打包
通过mvn package -P ${env} 加载不同配置文件 1.pom.xml中的配置 filter-dev.properties jdbc.properties