import configparser
config = configparser.ConfigParser() # 获得操作配置文件的句柄
config.read(r'aaa.ini') # 指定读取的配置文件
print(config.sections()) # 获得该配置文件的所有 sections
print(config.options('section1')) # 获得指定 section 下的所有 options
print(config.items('section1')) # 获得指定 section 下的所有键值对
print(config.get('section2', 'b1')) # 获得获得指定 section 下的 键 b1 对应的值
print(config.getint('section1', 'a1')) # 获得指定 section 下的 键 a1 对应的值,并转为整型
print(config.getfloat('section1', 'a2')) # 获得指定 section 下的 键 a2 对应的值,并转为浮点型
print(config.getboolean('section1', 'a3')) # 获得指定 section 下的 键 a3 对应的值,并转为布尔型
来源:https://www.cnblogs.com/caoyu080202201/p/12610090.html