导语:
本文主要介绍了关于Python如何读写配置文件?的相关知识,包括python读取配置文件,以及python实现文件读写这些编程知识,希望对大家有参考作用。
一个简单而又强大的读写配置文件的lib,
亮点在于自带的格式校验功能,并且支持复杂的嵌套格式,而且使用起来也相当的简便。
来看例子吧。
读文件
代码如下:
from configobj import ConfigObj
config = ConfigObj(filename)
#
value1 = config['keyword1']
value2 = config['keyword2']
#
section1 = config['section1']
value3 = section1['keyword3']
value4 = section1['keyword4']
#
# you could also write
value3 = config['section1']['keyword3']
value4 = config['section1']['keyword4']
写文件
代码如下:
from configobj import ConfigObj
config = ConfigObj()
config.filename = filename
#
config['keyword1'] = value1
config['keyword2'] = value2
#
config['section1'] = {}
config['section1']['keyword3'] = value3
config['section1']['keyword4'] = value4
#
section2 = {
'keyword5': value5,
'keyword6': value6,
'sub-section': {
'keyword7': value7
}
}
config['section2'] = section2
#
config['section3'] = {}
config['section3']['keyword 8'] = [value8, value9, value10]
config['section3']['keyword 9'] = [value11, value12, value13]
#
config.write()
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 如何用python模拟点击玩游戏?11/18
- ♥ 如何在php中使用python脚本文件的内容12/01
- ♥ Python3中类属性槽的常见问题有哪些?01/13
- ♥ 如何在python中安装keras10/15
- ♥ 如何在 Python 中读取 JSON 数据09/13
- ♥ 如何安装和配置python环境变量08/17
内容反馈