day5模块学习--configparser模块
使用ConfigParser模块读写ini文件(http://blog.csdn.net/linda1000/article/details/11729561)
ConfigParserPython的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。模块所解析的ini配置文件是由多个section构成,每个section名用中括号‘[]’包含,每个section下可有多个配置项类似于key-value形式,例如:
ConfigParser模块以ConfigParser类为例,其操作基本分为三类:1)初始化;2)读取配置;3)写入配置。
1. ConfigParser 初始化
使用ConfigParser 首选需要初始化实例,并读取配置文件:
cf = ConfigParser.ConfigParser() cf.read("配置文件名")
2.基本的读取配置文件
-read(filename)直接读取ini文件内容;
-sections()得到所有的section,并以列表的形式返回
-options(section)得到该section的所有option
-items(section)得到该section的所有键值对;
-get(section,option)得到section中的option的值,返回为string类型
-getint(section,option)得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat()函数。
3.基本的写入配置文件
-add_section(section)添加一个新的section;
-set(section,option,value)对section中的option进行设置,需要调用write将内容写入配置文件;
-write(strout)将对configparser类的修改写入。
例如:
test.conf #文件名
[sec_a]
a_key1
=
20
a_key2
=
10
[sec_b]
b_key1
=
121
b_key2
=
b_value2
b_key3
=
$r
b_key4
=
127.0
.
0.1
import configparser #初始化
cf = configparser.ConfigParser() #读取加载文件
cf.read('test.conf') # 读取配置文件
secs = cf.sections()
print("secs",secs)
opts = cf.options('sec_a')
print("options",opts) kvs = cf.items("sec_a")
print("sec_a",kvs) #read by type
str_val = cf.get("sec_a","a_key1")
int_val = cf.getint("sec_a","a_key2")
print("str_val",str_val)
print("int_val",int_val) #写入配置文件
#update value
cf.set("sec_b","b_key3","new_$r")
#set a new value
cf.set("sec_b","b_newkey","new_value")
#create a new section
cf.add_section("a_new_section")
cf.set("a_new_section","new_key","new_value") #write back to configure file
cf.write(open("test.conf","w"))
运行结果如下:
secs ['sec_a', 'sec_b']
options ['a_key1', 'a_key2']
sec_a [('a_key1', '20'), ('a_key2', '10')]
str_val 20
int_val 10
下面用python生成一个configparser的文件格式:
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)
最新文章
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
- Java中public,private,protected,和默认的区别
- session的一个问题
- Windows 10 上强制Visual Studio以管理员身份运行
- Linux下编译Boost
- JS代码片段:判断一个元素是否进入可视区域
- 转:PHP分页技术的代码和示例
- 解决ZBarSDK 中文出现乱码
- C# CookieHelper
- Spring-boot中使用@ConditionalOnExpression注解,在特定情况下初始化bean
- 汉字转拼音,TinyPinyin、Pinyin4j与JPinyin哪个库更快
- Example005控制弹出窗口居中显示
- Vue.js用法详解(一)更新中~
- 关于Mybatis的一次pingQuery时间间隔的实践及思考
- python 12
- MySQL入门很简单-触发器
- springboot-33-使用maven打瘦包
- java 多线程9 : synchronized锁机制 之 代码块锁
- Linux必知必会——od命令
- 【maven】maven 子项目如何使用父项目的jar包
热门文章
- ElasticStack系列之十四 &; ElasticSearch5.x bulk update 中重复 id 性能骤降
- P1099 树网的核 &;&; P2491 [SDOI2011]消防
- P2158 [SDOI2008]仪仗队 &;&; 欧拉函数
- NLP分词
- android listview使用自定义的adapter没有了OnItemClickListener事件解决办法
- PHP5下WSDL,SOAP调用实现过程
- [洛谷P2444] [POI2000]病毒
- How to Tell Science Stories with Maps
- CentOS 7快速入门系列教程(一)
- 【Swift】UIAlertController使用