Python模块: ConfigParser
生活随笔
收集整理的這篇文章主要介紹了
Python模块: ConfigParser
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在程序中使用配置文件來靈活的配置一些參數是一件很常見的事情,配置文件的解析并不復雜,在python里更是如此,在官方發布的庫中就包含有做這件事情的庫,那就是ConfigParser,這里簡單的做一些介紹。
??? ConfigParser解析的配置文件的格式比較象ini的配置文件格式,就是文件中由多個section構成,每個section下又有多個配置項,比如:
[db]
db_host=127.0.0.1
db_port=3306
db_user=root
db_pass=password
[concurrent]
thread=10
processor=20
??? 假設上面的配置文件的名字為test.conf。里面包含兩個section,一個是db, 另一個是concurrent, db里面還包含有4項,concurrent里面有兩項。這里來做做解析: 1 #-*- encoding: gb2312 -*- 2 import ConfigParser 3 import string, os, sys 4 5 cf = ConfigParser.ConfigParser() 6 cf.read("test.conf") 7 # 返回所有的section 8 s = cf.sections() 9 print 'section:', s 10 11 o = cf.options("db") 12 print 'options:', o 13 14 v = cf.items("db") 15 print 'db:', v 16 17 print '-'*60 18 #可以按照類型讀取出來 19 db_host = cf.get("db", "db_host") 20 db_port = cf.getint("db", "db_port") 21 db_user = cf.get("db", "db_user") 22 db_pass = cf.get("db", "db_pass") 23 24 # 返回的是整型的 25 threads = cf.getint("concurrent", "thread") 26 processors = cf.getint("concurrent", "processor") 27 28 print "db_host:", db_host 29 print "db_port:", db_port 30 print "db_user:", db_user 31 print "db_pass:", db_pass 32 33 print "thread:", threads 34 print "processor:", processors 35 #修改一個值,再寫回去 36 cf.set("db", "db_pass", "zhaowei") 37 cf.write(open("test.conf", "w"))
??? ConfigParser解析的配置文件的格式比較象ini的配置文件格式,就是文件中由多個section構成,每個section下又有多個配置項,比如:
[db]
db_host=127.0.0.1
db_port=3306
db_user=root
db_pass=password
[concurrent]
thread=10
processor=20
??? 假設上面的配置文件的名字為test.conf。里面包含兩個section,一個是db, 另一個是concurrent, db里面還包含有4項,concurrent里面有兩項。這里來做做解析: 1 #-*- encoding: gb2312 -*- 2 import ConfigParser 3 import string, os, sys 4 5 cf = ConfigParser.ConfigParser() 6 cf.read("test.conf") 7 # 返回所有的section 8 s = cf.sections() 9 print 'section:', s 10 11 o = cf.options("db") 12 print 'options:', o 13 14 v = cf.items("db") 15 print 'db:', v 16 17 print '-'*60 18 #可以按照類型讀取出來 19 db_host = cf.get("db", "db_host") 20 db_port = cf.getint("db", "db_port") 21 db_user = cf.get("db", "db_user") 22 db_pass = cf.get("db", "db_pass") 23 24 # 返回的是整型的 25 threads = cf.getint("concurrent", "thread") 26 processors = cf.getint("concurrent", "processor") 27 28 print "db_host:", db_host 29 print "db_port:", db_port 30 print "db_user:", db_user 31 print "db_pass:", db_pass 32 33 print "thread:", threads 34 print "processor:", processors 35 #修改一個值,再寫回去 36 cf.set("db", "db_pass", "zhaowei") 37 cf.write(open("test.conf", "w"))
?
參考:
def read_hotfix_cfg(cfg_file):print "----Reading the hotfix cfg file----"cfg_file = "./mgmt/hotfixes/"+cfg_fileif not os.path.isfile(cfg_file):print "The config file is not exist"return Falsetry:cfg = configobj.ConfigObj(cfg_file, encoding='UTF8')except Exception, e:print "The cfg_file is wrong: ",print ereturn Falseelements_path = []hf_elements = 'file_build_path'if cfg.has_key(hf_elements):elements_path = cfg[hf_elements].strip().split('\n')else:print "The [file_bulid_path] is not in the cfg file"return Falseglobal VERSIONS, SRC_TREEif cfg.has_key('versions'):VERSIONS = str(cfg['versions'])SRC_TREE = VERSIONS + "-rpl2sles-upgrade"else:print "The [versions] is not in the cfg file"return False?
轉載于:https://www.cnblogs.com/WayneZeng/p/9290743.html
總結
以上是生活随笔為你收集整理的Python模块: ConfigParser的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python模块: 命令行解析optio
- 下一篇: npm install 报错 :stac