configrw是一个基于键值或ini结构的简单读写器配置文件。
configrw的Python项目详细描述
配置w
configrw是一个简单的python模块,它根据键值或ini结构读写配置文件。
主要功能
- 支持非节(用于访问基于键值的简单配置文件)
- 在节中的任意字符串上插入选项
- 支持选项的多个值
- 支持没有值的选项
- 支持部分中的评论
- 支持选项、值的缩进
- 安全文件重写。写入时使用*.new file,然后重命名为原始文件名
安装
安装包到用户python库目录:
$ pip install --user configrw
或者可以将包安装到python库的全局系统目录(不推荐):
$ sudo pip install configrw
文档
您可以在readthedocs找到有关如何使用configrw的完整手册
快速启动
在下面的示例中,我们将使用以下ini文件:
# This is commentthis is option=this is valuesecond option=-100[ SECTION1 ] # commentoption1=100 option2 = 200 # comment option3 = 1.2[ section2 ]param1='str' param2 = 0 # comment parameter_without_value[section3]extensions= # comment1 ext1 # comment2 ext2 ext3
进入非截面积
如果要使用配置文件的简单键值,则需要此功能
fromconfigrwimportConfigconfig=Config.from_file('/path/to/file')section=config[None]# Getting non-sectionvalue=section['this is option']# Getting the valuesection['this is option']=None# Setting the valuedelsection['second option']# Deleting the option
进入分区
如果要使用ini配置文件,这是必需的功能
value=config['SECTION1']['option2']# Getting the valueconfig['SECTION1']['option2']=0# Setting the valueconfig['SECTION1']['option3']=300# Adding new option to sectionconfig['section3']['extensions'].append('ext4')# Adding new value to multiple valuesconfig['section3']['extensions'].insert('ext0',0)# Inserting new valueconfig['section3']['extensions'][0]='extension0'# Changing single value of multiple valuesconfig.write('/path/to/file')# Saving config to file# Render config to screenforlineinconfig.to_text():print(line)
更改后的ini文件:
# This is commentthis is option[ SECTION1 ] # commentoption1=100 option2 = 0 # comment option3 = 300[ section2 ]param1='str' param2 = 0 # comment parameter_without_value[section3]extensions= extension0 # comment1 ext1 # comment2 ext2 ext3 ext4