python封裝configparser模組擷取conf.ini值

來源:互聯網
上載者:User

標籤:圖片   迴圈   obj   span   comm   coding   ini   ons   space   

  configparser模組是python內建的從檔案中擷取固定格式參數的模組,因為是python只帶的,大家用的應該很多,我覺得這個參數模組比較靈活,添加參數、修改參數、讀取參數等都有對應的參數供使用者使用。因為本人看的都是接近自動化架構方面的,主要用讀取參數,手動操作添加和修改參數也許更方便。

  configparser模組讀取的參數應該是不限文檔格式:*.ini、*.conf、*.*,但是內容格式要按照固定格式來進行填寫:

  [section]

  key = value

 1 #路徑參數 2 [path] 3 my_dir = D:\python\workspace\pythontest 4 log_path = %(my_dir)s\\log\ 5 img_path = %(my_dir)s\screenshot 6 report_path = %(my_dir)s\\report\ 7  8 #郵箱參數 9 [email]10 uesr = 123456789011 password = 12345612 smtphost = [email protected]163.com13 from = 123123@163.com14 to = [email protected]15 16 #日誌參數17 [log]18 logtime = 11232319 20 #測試參數21 [main]22 ip = 109.132.19.723 port = 990024 url = http://%(ip)s:%(port)s/tes

  封裝後的代碼:

 1 #coding:utf-8 2 import ConfigParser 3 import os 4  5 class ReadConfig(object): 6     ‘‘‘配置參數‘‘‘ 7     def __init__(self): 8         ‘‘‘ 9         用法:10         conf = ReadConfig()11         confinfo = conf.getconf("url")12         ‘‘‘13         #設定conf.ini路徑14         current_dir = os.path.dirname(__file__)15         top_one_dir = os.path.dirname(current_dir)16         file_name = top_one_dir + "\\conf\\conf.ini"17         #執行個體化ConfigParser對象18         self.config = ConfigParser.ConfigParser()19         self.config.read(file_name)20 21     def getconf(self, keyname):22         num = len( self.config.sections() )23         i = 024         print("num = ",num)25         #根據sections的數量進行迴圈尋找keyname是否存在26         while i < num :27             # 每次迴圈擷取對應list中的section28             section = self.config.sections()[i]29             #如果keyname存在section中,則輸出其參數值30             if keyname in self.config.options(section):31                 pass32                 print("num --> ",i)33                 print("section --->",section)34                 print("keyname ---> ", keyname)35                 self.info = self.config.get(section, keyname)36                 break37             else :38                 i = i + 139         return self.info40 41 if __name__ == "__main__":42     conf = ReadConfig()43     confinfo = conf.getconf("url")44     print("config --- >",confinfo)

  一開始本想通過section值擷取對應的key以及value,但是試了很多次都不成功。原設想:

1     conf = ReadConfig()2     confinfo = conf.getconf("main").url

  main為section,url為key。

  但是這種通過key擷取參數value的方法也可以實現我的目的。

1     conf = ReadConfig()2     confinfo = conf.getconf("url")

  url為key

運行結果:

  ini檔案存放在pythontest/conf下

  封裝後的py檔案存放在pythontest/commlib下

 

python封裝configparser模組擷取conf.ini值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.