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模块的更多相关文章

  1. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  2. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

  3. python中configparser模块读取ini文件

    python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...

  4. python 的ConfigParser模块

    Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

  5. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  6. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  7. Python操作配置文件configparser模块

    在实际的开发过程中,我们常有操作ini格式和conf格式配置文件的操作,Python为我们提供了configparser模块,方便我们对配置文件进行读写操作. config.ini配置文件内容如下: ...

  8. Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件

    前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...

  9. python中configparser模块的使用

    configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...

随机推荐

  1. oracle创建默认表空间---重要

    当oracle创建数据库后,sys创建用户时还要有默认表空间.不创建默认表空间在导如项目时会有些数据表导入不成功! 由于时间仓促以截屏为例  之后会在刚刚那个空文件生成一个文件 ----------- ...

  2. bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】

    正反加边分别跑spfa最短路,把两次最短路的和求个max就是答案 #include<iostream> #include<cstdio> #include<queue&g ...

  3. python之Beautiflusoup操作

    from bs4 import BeautifulSoupimport requestsimport os ######对风景进行爬出操作r = requests.get("http://6 ...

  4. Hadoop回收站及fs.trash参数详解

    前言: Linux系统里,个人觉得最大的不方便之一就是没有回收站的概念.rm -rf很容易造成极大的损失.而在Hadoop或者说HDFS里面,有trash(回收站)的概念,可以使得数据被误删以后,还可 ...

  5. 台哥原创:java 连连看源码

    2010年,迷上了玩连连看 随手就做了这个,正好手头有这些图片素材 ​ 游戏启动时,界面先铺上了一层透明幕布,然后这些兵器图片交替从上到下,从左到右出现.. ​ 鼠标停在兵器格子上时,所在格子会有红色 ...

  6. Can't install '*' from pristine store, because no checksum is recorded for this file (SVN报错)

    问题:同步.cleanup都会出现下面的提示 svn: E155017: Can't install '*' from pristine store, because no checksum is r ...

  7. 洛谷 P1582 倒水

    题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒 ...

  8. 创建 pulic dblink 使不同的用户可以访问dblink

    1. system: grant create public database link to ivrsdata; 2.userdate: tnsnames.ora config db connect ...

  9. [转]深入ASP.NET MVC之二:路由模块如何工作

    本文转自:http://www.cnblogs.com/yinzixin/archive/2012/11/05/2754483.html 摘要: 上文分析了UrlRouting模块何时会被触发,本文重 ...

  10. microPython 的逗比报错的问题

    今天搞了一天,发现了各种问题,首先最终的解决办法就是重现刷固件!!!! 重刷固件就需要清除flash! cd C:\Users\sansong\AppData\Local\Programs\Pytho ...