https://www.cnblogs.com/xuexianqi/p/12603846.html

import configparser

config = configparser.ConfigParser()
config.read('test.ini')
import configparser

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

# 1.获取sections
print(config.sections())    # ['section1', 'section2']

# 2.获取某一sections下的所有的option
print(config.options('section1'))   # ['k1', 'k2', 'user', 'age', 'is_admin', 'salary']

# 3.获取items
print(config.items('section1'))     # [('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]

# 4.获取某个section单独的元素值
res = config.get('section1', 'user')
print(res, type(res))   # egon <class 'str'>

res1 = config.getint('section1', 'age')
print(res1, type(res1)) # 18 <class 'int'>

res2 = config.getboolean('section1', 'is_admin')
print(res2, type(res2)) # True <class 'bool'>

res3 = config.getfloat('section1', 'salary')
print(res3, type(res3)) # 31.0 <class 'float'>

相关文章:

  • 2021-06-29
  • 2021-11-17
  • 2021-08-06
  • 2022-12-23
猜你喜欢
  • 2021-09-10
  • 2021-05-16
  • 2021-06-25
  • 2021-06-07
  • 2021-08-07
  • 2021-05-29
相关资源
相似解决方案