1. 程式人生 > >Python ConfigParser的使用

Python ConfigParser的使用

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型別,還有相應的getboolean()和getfloat() 函式。 2.基本的寫入配置檔案 -add_section(section) 新增一個新的section -set( section, option, value) 對section中的option進行設定,需要呼叫write將內容寫入配置檔案。 3.基本例子 test.conf [sec_a]  a_key1 = 20  a_key2 = 10  [sec_b]  b_key1 = 121  b_key2 = b_value2  b_key3 = $r  b_key4 = 127.0.0.1  parse_test_conf.py import ConfigParser  cf = ConfigParser.ConfigParser()  #read config cf.read("test.conf")  # return all section secs = cf.sections()  print 'sections:', secs  opts = cf.options("sec_a")  print 'options:', opts  kvs = cf.items("sec_a")  print 'sec_a:', kvs  #read by type str_val = cf.get("sec_a", "a_key1")  int_val = cf.getint("sec_a", "a_key2")  print "value for sec_a's a_key1:", str_val  print "value for sec_a's a_key2:", int_val  #write config #update value cf.set("sec_b", "b_key3", "new-$r")  #set a new value cf.set("sec_b", "b_newkey", "new-value")  #create a new section cf.add_section('a_new_section')  cf.set('a_new_section', 'new_key', 'new_value')  #write back to configure file cf.write(open("test.conf", "w"))  得到終端輸出: sections: ['sec_b', 'sec_a']  options: ['a_key1', 'a_key2']  sec_a: [('a_key1', "i'm value"), ('a_key2', '22')]  value for sec_a's a_key1: i'm value  value for sec_a's a_key2: 22  更新後的test.conf [sec_b]  b_newkey = new-value  b_key4 = 127.0.0.1  b_key1 = 121  b_key2 = b_value2  b_key3 = new-$r  [sec_a]  a_key1 = i'm value  a_key2 = 22  [a_new_section]  new_key = new_value  4.Python的ConfigParser Module中定義了3個類對INI檔案進行操作。分別是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基礎的INI檔案讀取類,ConfigParser、SafeConfigParser支援對%(value)s變數的解析。  設定配置檔案test2.conf [portal]  url = http://%(host)s:%(port)s/Portal  host = localhost  port = 8080  使用RawConfigParser: import ConfigParser  cf = ConfigParser.RawConfigParser()  print "use RawConfigParser() read" cf.read("test2.conf")  print cf.get("portal", "url")  print "use RawConfigParser() write" cf.set("portal", "url2", "%(host)s:%(port)s")  print cf.get("portal", "url2")  得到終端輸出: use RawConfigParser() read  http://%(host)s:%(port)s/Portal  use RawConfigParser() write  %(host)s:%(port)s 改用ConfigParser: import ConfigParser  cf = ConfigParser.ConfigParser()  print "use ConfigParser() read" cf.read("test2.conf")  print cf.get("portal", "url")  print "use ConfigParser() write" cf.set("portal", "url2", "%(host)s:%(port)s")  print cf.get("portal", "url2")  得到終端輸出: use ConfigParser() read  http://localhost:8080/Portal  use ConfigParser() write  localhost:8080 改用SafeConfigParser: import ConfigParser  cf = ConfigParser.SafeConfigParser()  print "use SafeConfigParser() read" cf.read("test2.conf")  print cf.get("portal", "url")  print "use SateConfigParser() write" cf.set("portal", "url2", "%(host)s:%(port)s")  print cf.get("portal", "url2")  得到終端輸出(效果同ConfigParser): use SafeConfigParser() read  http://localhost:8080/Portal  use SateConfigParser() write  localhost:8080  http://www.linuxso.com/linuxbiancheng/8987.html

相關推薦

python-ConfigParser模塊【讀寫配置文件】

target new start 需要 details string 如何 設置變量 board http://www.codesky.net/article/201003/122500.html http://www.linuxso.com/linuxbiancheng/

Python Configparser模塊讀取、寫入配置文件

con 返回 utf python .get pen Coding move odi 寫代碼中需要用到讀取配置,最近在寫python,記錄一下。 如下,假設有這樣的配置。 [db] db_host=127.0.0.1 db_port=3306 db_use

Python - configParser模塊學習

增加 解析 utf-8 country 節點 一起 get col 配置 configParser 模塊用於操作配置文件 註:Parser漢譯為“解析”之意。 配置文件的格式與windows ini文件類似,可以包含一個或多個節(section)

python ConfigParser 配置讀寫

file logs 整型 csdn 文件的 設置 bsp 記得 列表 我們寫程序的時候一般都會寫程序包的時候,很少硬編碼寫在一起,這有個很好的配置文件包。 參考 ConfigParser 和 ConfigParser 的使用 一、ConfigParser 簡介 Con

Python configparser模塊

python3.x imp 生成 9.png tab wid mage col parse configparser模塊 一.configparser模塊 用於生成和修改常見配置文檔,但那個錢模塊名稱在python3.x版本中變更為configpar

python--ConfigParser讀寫改配置文件

得到 pen with open ring 文件 open 打開 work add from configparser import ConfigParser fp = ‘conf.ini‘ #定義配置文件名 conf = ConfigParser() #實例化

python--configparser

conf pri alt 配置文件 close post val 特定 lap configparser 是Python自帶的模塊, 用來讀寫配置文件。   特定的格式: 1 # 註釋1 2 ; 註釋2 3 [Node1] #節點 4 key1=value

python configparser 模塊

spa fault rem 修改配置 bsp true fig remove 版本 configparser 模塊是用於生成和修改常見配置文檔,當前模塊的名稱在 python 3.x 版本中變更為 configparser。 示例配置文件 [DEFAULT] Server

python configparser

__name__ ger DBName name == get imp end items import configparser##用於操作配置文件,格式與windows ini文件類似,可以包含一個或多個節(section),每個節可以有多個參數(鍵=值)con = c

python-configparser模塊

UC value bucket uri buck class fig cti bit import configparser config = configparser.ConfigParser() #config={}#生成config空字典 config = {} c

python configparser模塊的應用

item move AR 分隔 not section 指定 windows set方法 import configparser"""“[ ]”包含的為 section,section 下面為類似於 key - value 的配置內容;configparser 默認支持 ‘

python ConfigParser讀取配置文件,及解決報錯ConfigParser.MissingSectionHeaderError: File contains no section headers的方法

一個 Coding parse repo 報錯 sel 解決方案 ins def 先說一下在讀取配置文件時報錯的問題--ConfigParser.MissingSectionHeaderError: File contains no section headers 問題描述

python configparser配置檔案解析器使用詳解

configparser簡介 python2下該模組名為ConfigParser,到3才改為configparser,可以看官方ConfigParser模組的說明 ConfigParse 官方文件 python3中configparser模組的使用,confi

python-ConfigParser模組【讀寫配置檔案】

以下的文章就是對python 讀寫配置檔案的具體方案的介紹 1,函式介紹 1.1.讀取配置檔案 -read(filename) 直接讀取ini檔案內容 -sections() 得到所有的section,並以列表的形式返回 -options(section) 得到

Python ConfigParser模組

Python 讀取寫入配置檔案 —— ConfigParser   Python 讀取寫入配置檔案很方便,可使用內建的 configparser 模組;可檢視原始碼,如博主本機地址: “C:/python27/lib/configparser.py” Configu

python ConfigParser 區分大小寫

使用ConfigParser時發現在sections下面的options不會區分大小寫,原因為ConfigParser設定預設為識別小寫,自己從新改下optionxform即可 class MyConfigParser(ConfigParser.ConfigParser):

Python configparser模塊 與 subprocess 模塊

mod 再次 man none command pow for 錯誤輸出 options configparser 模塊 Python中 configparser 模塊用於讀取和編輯配置文件,更多的是用於讀取配置文件。配置文件的格式如下,可以包含多個section(例如:d

Python ConfigParser的使用

1.基本的讀取配置檔案 -read(filename) 直接讀取ini檔案內容 -sections() 得到所有的section,並以列表的形式返回 -options(section) 得到該section的所有option -items(section) 得到該section的所有鍵值對 -get(se

python ConfigParser庫使用和遇到的坑

背景:這幾天想在介面測試中增加logging列印功能,在testerHome正好發現有人分享自己的介面框架,看了之後其中用到ConfigParser,然後自己寫了遇到一個坑,寫下來用於備註吧。 首先就描述一下坑:ConfigParser主要就是讀寫配置檔案,檔案格式包括.i

python configparser讀取、寫入、修改配置檔案conf,ini

使用python,呼叫configparser類庫, 對conf,ini配置檔案進行操作 寫出的配置檔案內容如下: # -*- coding: utf-8 -*- """ /****************************************