天天看點

python學習筆記之讀取配置檔案

1.配置一個.int檔案用來寫配置檔案,例如

2.基本的讀操作

-read(filename)               直接讀取檔案内容

-sections()                      得到所有的section,并以清單的形式傳回

-options(section)            得到該section的所有option

-items(section)                得到該section的所有鍵值對

-get(section,option)        得到section中option的值,傳回為string類型

-getint(section,option)    得到section中option的值,傳回為int類型,還有相應的getboolean()和getfloat() 函數。

在對配置檔案進行讀寫操作前,我們需要先進行以下兩個操作:

執行個體化ConfigParser對象:  

         2.讀取配置檔案

然後進行配置檔案的讀取操作

基本的寫入操作:

-write(fp)  将config對象寫入至某個 .init 格式的檔案  Write an .ini-format representation of the configuration state.

-add_section(section)   添加一個新的section

-set( section, option, value   對section中的option進行設定,需要調用write将内容寫入配置檔案

-remove_section(section)  删除某個 section

-remove_option(section, option) 

配置檔案中的名字是不區分大小寫的,如下兩個是等價的