day6_python之configparser_模块

configparser_模块是为了解析.ini文件的配置

a.ini

[xiechao]

name=xiechao
age=18
is_admin=True

salary=1000000.12



[xiemingyuan]

name=xiechao
age=3
is_admin=True

salary=False

1、查看a.ini

import configparser


config = configparser.ConfigParser()
config.read('a.ini')

print(config.sections()) #看标题,看整个文件中有多少个标题

print(config.options(config.sections()[0]))  #查看第一个标题下边的配置项有哪些

print(config.get('xiechao','name'))#查看某个标题下的某个配置项的值

res = config.get('xiechao','name')
print(type(res)) #<class 'str'>

res = config.getint('xiechao','age')

 2、修改

config.remove_section('xiechao')   #xiechao整个标题就被删除


config.remove_section('xiechao','age')  #删除xiechao下面的age选项


config.write(open('a.ini','w'))

3、增加 

config.add_section('xiechao')  #增加配置,先加标题
config.set('xiechao','age',18)  #添加选项,第一个参数是标题,第二个是k ,第三个是v
config.write(open('a.ini','w'))

  

原文地址:https://www.cnblogs.com/xiechao621/p/7887337.html