configparser模块

import configparser

config=configparser.ConfigParser()
config.read('config.ini') #a.cfg a.ini a.cnf


# 并不是字典 key = value 形式
# print(config.sections()) # 查看所有的标题
# print(config.options('egon')) # 查看标题egon下所有的key
# print(config.items('egon')) # 查看标题egon下所有的key,value


# res=config.get('egon','age') # 查看标题egon下age的值,字符串格式
# res=config.getint('egon','age') # 查看标题egon下age的值,整型格式
# print(res,type(res))

# res=config.getfloat('egon','salary')# 浮点型
# print(res,type(res))

# res=config.getboolean('egon','is_beautiful')# 布尔型
# print(res,type(res))

附上文件:
  
  
[egon]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31

[alex]
k1 = v1
原文地址:https://www.cnblogs.com/Roc-Atlantis/p/9225315.html