新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
怎么在Python中使用configparser庫讀取配置文件-創(chuàng)新互聯(lián)
怎么在Python中使用configparser庫讀取配置文件?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
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)
關(guān)于怎么在Python中使用configparser庫讀取配置文件問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道了解更多相關(guān)知識。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文名稱:怎么在Python中使用configparser庫讀取配置文件-創(chuàng)新互聯(lián)
當(dāng)前地址:http://fisionsoft.com.cn/article/dgisjh.html