python解析模块(ConfigParser)使用方法
时间:2022-06-15 11:39:06
测试配置文件test.conf内容如下:
[first]
w = 2
v: 3
c =11-3
[second]
sw=4
test: hello
测试配置文件中有两个区域,first和second,另外故意添加一些空格、换行。
下面解析:
>>> import ConfigParser
>>> conf=ConfigParser.ConfigParser()
>>> conf.read('test.conf')
['test.conf']
>>> conf.sections() #获得所有区域
['first', 'second']
>>> for sn in conf.sections():
... print conf.options(sn) #打印出每个区域的所有属性
...
['w', 'v', 'c']
['sw', 'test']
获得每个区域的属性值:
for sn in conf.sections():
print sn,'-->'
for attr in conf.options(sn):
print attr,'=',conf.get(sn,attr)
输出:
first -->
w = 2
v = 3
c = 11-3
second -->
sw = 4
test = hello
好了,以上就是基本的使用过程,下面是动态的写入配置,
cfd=open('test2.ini','w')
conf=ConfigParser.ConfigParser()
conf.add_section('test') #add a section
conf.set('test','run','false')
conf.set('test','set',1)
conf.write(cfd)
cfd.close()
上面是向test2.ini写入配置信息。
标签:配置解析模块,ConfigParser
0
投稿
猜你喜欢
Python字符串hashlib加密模块使用案例
2023-08-02 12:06:24
python3使用print打印带颜色的字符串代码实例
2022-01-20 10:30:49
Python实现用户名和密码登录
2022-02-17 13:49:49
有序列表 li ol
2008-07-30 12:31:00
CSS自适应宽度圆角按钮
2007-11-20 11:38:00
Python操作MongoDB数据库PyMongo库使用方法
2023-03-16 00:13:14
基于javascript如何传递特殊字符
2023-09-06 04:14:23
SQL Server中实现二进制与字符类型之间的数据转换
2023-07-17 21:34:47
python 计算t分布的双侧置信区间
2023-08-01 03:06:05
Python处理时间日期坐标轴过程详解
2021-04-28 08:05:27
举例讲解Python中的死锁、可重入锁和互斥锁
2023-12-21 07:35:03
图片预加载效果的实现
2008-06-16 12:08:00
在Python3.74+PyCharm2020.1 x64中安装使用Kivy的详细教程
2023-01-16 15:17:39
使用Python opencv实现视频与图片的相互转换
2022-03-04 15:20:31
Go语言Grpc Stream的实现
2023-08-07 06:19:23
PHP addslashes()函数讲解
2023-06-04 04:28:24
如何在SQL2000的查询中使用XML-Data?
2010-06-18 19:26:00
使用ASP遍历并列表显示目录文件
2009-11-08 18:32:00
Python函数装饰器实现方法详解
2023-08-10 12:33:16
MySQL5创建存储过程实例
2010-06-13 12:49:00