configparser解析配置文件

1.配置文件

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

2.解析配置文件

>>> import configparser # 导入模块
>>> config = configparser.ConfigParser()  #实例化(生成对象)
>>> config.sections()  #调用sections方法
[]
>>> config.read(‘example.ini‘)  # 读配置文件(注意文件路径)
[‘example.ini‘]
>>> config.sections() #调用sections方法(默认不会读取default)
[‘bitbucket.org‘, ‘topsecret.server.com‘]
>>> ‘bitbucket.org‘ in config #判断元素是否在sections列表内
True
>>> ‘bytebong.com‘ in config
False
>>> config[‘bitbucket.org‘][‘User‘] # 通过字典的形式取值
‘hg‘
>>> config[‘DEFAULT‘][‘Compression‘]
‘yes‘
>>> topsecret = config[‘topsecret.server.com‘]
>>> topsecret[‘ForwardX11‘]
‘no‘
>>> topsecret[‘Port‘]
‘50022‘
>>> for key in config[‘bitbucket.org‘]: print(key) # for循环 bitbucket.org 字典的key,注意,DEFAULT中的也会出来,因为DEFAULT中的配置信息默认就是给下面的模块中的内容使用的
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config[‘bitbucket.org‘][‘ForwardX11‘]
‘yes‘

3.其他增删改查方法

 [group1] # 支持的两种分隔符“=”, “:”
k1 = v1
k2:v2

[group2]
k1 = v1

>>> import configparser

>>> config = configparser.ConfigParser()
>>> config.read(‘i.cfg‘)
[‘i.cfg‘]

# ########## 读 ##########
>>> secs = config.sections()
>>> print(secs)
[‘group1‘, ‘group2‘]

>>> options = config.options(‘group2‘) # 获取指定section的keys
>>> print(options)
[‘k1‘]

>>>item_list = config.items(‘group2‘) # 获取指定 section 的 keys & values ,key value 以元组的形式
>>>print(item_list)
[(‘k1‘, ‘v1‘)]

>>>val = config.get(‘group1‘,‘k1‘) # 获取指定的key 的value
>>> print(val)
v1

>>>val = config.getint(‘group1‘,‘key‘)

# ########## 改写 ##########
>>>sec = config.remove_section(‘group1‘) # 删除section 并返回状态(true, false)
>>> print(sec)
True

>>>config.write(open(‘i.cfg‘, "w")) # 对应的删除操作要写入文件才会生效

>>>sec = config.has_section(‘vita‘)
>>> print(sec)
False

>>>sec = config.add_section(‘vita‘)
>>>config.write(open(‘i.cfg‘, "w")) #
查看内容
[group2]
k1 = v1

[vita]

>>>config.set(‘group2‘,‘k1‘,"11111")
>>>config.set(‘group2‘,‘k1‘,"2222")
>>>config.write(open(‘i.cfg‘, "w"))
查看内容
[group2]
k1 = 11111
k2 = 222

[vita]

>>>config.remove_option(‘group2‘,‘age‘)
>>>config.write(open(‘i.cfg‘, "w"))

原文地址:https://blog.51cto.com/10983441/2389802

时间: 2024-08-02 04:24:35

configparser解析配置文件的相关文章

[Python]ConfigParser解析配置文件

最近发现很多接口配置都硬编码在souce file中了,于是就看了下python怎么解析配置文件,重构下这一块. 这个应该是早就要作的... [mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid skip-external-locking old_passwords = 1 skip-bdb skip-innodb users = aa,bb,cc [names] n1 = lzz n2 = orangle n3 = zero e

python3使用configparser解析配置文件

http://www.jb51.net/article/87402.htm 需要注意的是每一个字段后面的值外面没有引号,切记,自己第一次配置时,加了引号,搞了半天 没找到错误,, 在用Python做开发的时候经常会用到数据库或者其他需要动态配置的东西,硬编码在里面每次去改会很麻烦.Python自带有读取配置文件的模块ConfigParser,使用起来非常方便. ini文件ini配置文件格式: 读取配置文件: 1 2 3 4 5 6 7 import ConfigParser conf = Con

python ConfigParser模块 配置文件解析

ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic

ConfigParser 读写配置文件

一.ini: 1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式 2.ini文件创建方法: (1)先建立一个记事本文件.(2)工具 - 文件夹选项 - 查看 - 去掉"隐藏已知文件的扩展名"前面的√.这样一来,你建立的那个记事本的扩展名就显示出来了"*.txt".然后,你把这个.txt扩展名更改为.ini 3.ini文件的格式: (1)节:section 节用方括号括起来,单独占一行,例如:

struct2源码解读(3)之解析配置文件

struct2源码解读之解析配置文件 上篇博文讲到struct2在初始化时,在创建Dispacher之后,会Dispacher.init()中会对配置文件进行解析,下面就让我们一起探讨下struct2是如何解析配置文件的. public Dispatcher initDispatcher( HostConfig filterConfig ) {           //创建Dispacher实例         Dispatcher dispatcher = createDispatcher(f

spring源码(3)之解析配置文件的过程

spring源码之解析配置文件过程 上篇博文,我们探讨了spring获取配置文件applicationContext.xml的document对象.回想struct2解析struct*.xml,当struct2获取struct*.xml文件的document对象之后,就会循环遍历这个document,然把不同的标签的信息封装到不同的对象中,如<package>标签封装到packageConfig对象,<action>标签封装到actionConfig对象等等.那么spring在获取

解析配置文件

fopen打开配置文件,不需要绝对路径,可以使相对路径 fgets按行读取文件 while(fgets(buf, 1024, fptr)){ } strtok_r或者strtok解析参数 int main(int argc, char *argv[]) { char *str1, *str2, *token, *subtoken; char *saveptr1, *saveptr2; int j; if (argc != 4) { fprintf(stderr, "Usage: %s strin

Android init进程——解析配置文件

目录 目录 init解析配置文件 关键字定义 kw_is 解析 K_import K_on command执行 K_service service service结构体 parse_service parse_line_service init控制service init解析配置文件 在解析service服务是如何启动之前,让我们先来学习一下init进程是如何解析init.rc等配置文件的. init进程解析配置文件的代码如下(/system/core/init/init.c&&/syst

mybatis 解析配置文件(一)之XML的DOM解析方式

简介 在之前的文章<mybatis 初步使用(IDEA的Maven项目, 超详细)>中, 讲解了mybatis的初步使用, 并总结了以下mybatis的执行流程: 通过 Resources 工具类读取 mybatis-config.xml, 存入 Reader: SqlSessionFactoryBuilder使用上一步获得的reader创建SqlSessionFactory对象; 通过 sqlSessionFactory 对象获得SqlSession; SqlSession对象通过selec