怎么在Python中使用configparser库读取配置文件-创新互联
怎么在Python中使用configparser库读取配置文件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

from configparser import ConfigParser
class DoConfig:
def __init__(self,filepath,encoding='utf-8'):
self.cf = ConfigParser()
self.cf.read(filepath,encoding)
#获取所有的section
def get_sections(self):
return self.cf.sections()
#获取某一section下的所有option
def get_option(self,section):
return self.cf.options(section)
#获取section、option下的某一项值-str值
def get_strValue(self,section,option):
return self.cf.get(section,option)
# 获取section、option下的某一项值-int值
def get_intValue(self, section, option):
return self.cf.getint(section, option)
# 获取section、option下的某一项值-float值
def get_floatValue(self, section, option):
return self.cf.getfloat(section, option)
# 获取section、option下的某一项值-bool值
def get_boolValue(self, section, option):
return self.cf.getboolean(section, option)
def setdata(self,section,option,value):
return self.cf.set(section,option,value)
if __name__ == '__main__':
cf = DoConfig('demo.conf')
res = cf.get_sections()
print(res)
res = cf.get_option('db')
print(res)
res = cf.get_strValue('db','db_name')
print(res)
res = cf.get_intValue('db','db_port')
print(res)
res = cf.get_floatValue('user_info','salary')
print(res)
res = cf.get_boolValue('db','is')
print(res)
cf.setdata('db','db_port','3306')
res = cf.get_strValue('db', 'db_port')
print(res)关于怎么在Python中使用configparser库读取配置文件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联成都网站设计公司行业资讯频道了解更多相关知识。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站名称:怎么在Python中使用configparser库读取配置文件-创新互联
标题路径:http://www.jxjierui.cn/article/dgisjh.html


咨询
建站咨询
