Python中ConfigParser模組的使用

2021-09-01 13:58:42 字數 1174 閱讀 1238

簡略介紹:configparser模組是用來處理配置檔案的。將配置項專門放到乙個配置檔案裡是個好習慣,用configparser模組可以很方便地對配置檔案進行修改。相應的,配置檔案也要遵循乙個標準格式。configparser有read()方法,用於讀取配置檔案,sections()方法,用於獲取所有小節,options(section)用於獲取指定小節的所有配置項,get(section,option)獲取指定配置項的值,set(section,option,value)用於設定指定配置項的值。

***********************************===

配置檔案 abc :

主檔案:

函式使用:

1.讀取配置檔案

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

-sections() 得到所有的section,並以列表的形式返回

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

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

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

-getint(section,option)得到section中option的值,返回為int型別

2.寫入配置檔案

-add_section(section)新增乙個新的section

-set( section, option, value)對section中的option進行設定

需要呼叫write將內容寫入配置檔案。

3.例子(略)

python讀取配置檔案configparser

可讀取寫入配置檔案 import configparser import os import sys class testcfigparser object config configparser.configparser def get value self root dir os.path.di...

python配置檔案讀寫(ConfigParse)

ini檔案格式 section0 key0 value0 key1 value1 section1 key2 value2 key3 value示例 config num 40column 8create size x 400create size y 300 color 深藍 dark blue ...

python 配置檔案讀取configparser

import configparser cp configparser.configparser 例項化 cp.read config.conf encoding utf 8 讀取配置檔案,允許讀取多個配置檔案,用列表 section 裡面的 獲取所有的section f cp.sections p...