python之configparser模块

configparser模块解析配置文件,解析内容格式:

[section1]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31

[section2]
k1 = v1

config=configparser.ConfigParser()      #拿到一个对象

config.read('config.ini')                         #a.cfg a.ini a.cnf文件后缀

print(config.sections())                                 #查看所有标题

print(config.options('section1'))                   #查看标题下的所有的key

print(config.items('section1'))                 #查看标题下面的所有key=value 以列表方式显示

#[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]

print(config.get('section1','user'))            #查看标题section1下user的值=>字符串格式

print(config.getint('section1','age'))          #查看标题section1下user的值=>整数格式

原文地址:https://www.cnblogs.com/Marcki/p/10111948.html