小白的Python之路 day5 configparser模块的特点和用法
生活随笔
收集整理的這篇文章主要介紹了
小白的Python之路 day5 configparser模块的特点和用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
configparser模塊的特點和用法
一、概述
主要用于生成和修改常見配置文件,當前模塊的名稱在 python 3.x 版本中變更為 configparser。在python2.x版本中為ConfigParser
二、格式
常見配置文件格式如下:
1 [DEFAULT] 2 serveraliveinterval = 45 3 compression = yes 4 compressionlevel = 9 5 forwardx11 = yes 6 7 [bitbucket.org] 8 user = hg 9 10 [topsecret.server.com] 11 host port = 50022 12 forwardx11 = no三、主要用法?
1、創建配置文件
1 import configparser #導入configparser模塊 2 3 #生成一個對象 4 config = configparser.ConfigParser() 5 #配置默認全局配置組 6 config["DEFALUT"] = {"ServerAliveInterval":"45", 7 "Compression":"yes", 8 "CompressionLevel":"9" 9 } 10 #配置第一個其他組 11 config["bitbucket.org"] = {} 12 #直接賦值 13 config["bitbucket.org"]["User"] = 'hg' 14 15 #配置第二個其他組 16 config["topsecret.server.com"] = {} 17 #這邊就賦給一個變量 18 topsecret = config["topsecret.server.com"] 19 #通過變量賦值 20 topsecret["Host Port"] = '50022' 21 topsecret["ForwardX11"] = 'no' 22 #給全局配置組賦值 23 config["DEFALUT"]["ForwardX11"] = "yes" 24 #操作完畢,把配置的內容寫入一個配置文件中 25 with open("example.ini","w") as configfile: 26 config.write(configfile)?
2、讀取配置文件
1)、讀取配置組
1 >>> import configparser 2 >>> config = configparser.ConfigParser() 3 >>> config.sections() #不讀取配置文件,組名列表為空 4 [] 5 >>> config.read("example.ini") #讀取配置文件,返回配置文件名 6 ['example.ini'] 7 >>> config.sections() #返回除默認配置組的其他組名 8 ['bitbucket.org', 'topsecret.server.com'] 9 >>> config.defaults() #讀取默認配置組,并返回有序字典 10 OrderedDict([('compressionlevel', '9'), ('serveraliveinterval', '45'), ('compression', 'yes'), ('forwardx11', 'yes')])2)、組名是否存在
1 >>> 'bitbucket.org' in config #組名存在 2 True 3 >>> 'zhangqigao.org' in config #組名不存在 4 False?
3)、讀取組內的值
1 >>> config["bitbucket.org"]["User"] #讀取"bitbucket.org"配置組中的值 2 'hg' 3 >>> config["DEFAULT"]["Compression"] #讀取默認配置組中的值 4 'yes' 5 >>> topsecret = config['topsecret.server.com'] #把配置組賦給一個對象 6 >>> topsecret['ForwardX11'] #通過對象獲取值?
4)、?循環獲取組內的key值
1 >>> for key in config["bitbucket.org"]: #循環打印bitbucket.org組下的key值 2 ... print(key) 3 ... 4 #輸出,只打印默認組和bitbucket.org組的key值 5 user 6 compressionlevel 7 serveraliveinterval 8 compression 9 forwardx11 10 >>> for key in config["topsecret.server.com"]:#循環打印topsecret.server.com組下的key值 11 ... print(key) 12 ... 13 #輸出,只打印默認組和topsecret.server.com組的key值 14 host port 15 forwardx11 16 compressionlevel 17 serveraliveinterval 18 compression重點:
四、configparser增刪改查語法
1、配置文件名i.cfg
| 1 2 3 4 5 6 7 8 9 10 | [DEFAULT] k1?=?v1 k2?=?v2 [section1] k3?=?v3 k4:v4 [section2] k5?=?5 |
2、讀i.cfg
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import?configparser config?=?configparser.ConfigParser() config.read("i.cfg") sec?=?config.sections() print(sec) #輸出 ['section1',?'section2'] options?=?config.options("section2")??#返回默認組和section2組的key值 print(options) #輸出 ['k5',?'k1',?'k2'] item_list?=?config.items("section2")???#返回默認組和section2組的key-value值 print(item_list) #輸出 [('k1',?'v1'), ('k2',?'v2'), ('k5',?'5')] val1?=?config.get("section2","k1")???#獲取section2組中k1對應的值,是否可取是按照上面返回的列表 print(val1) #輸出 v1 val2??=?config.getint("section2","k5")??#返回section2中k5的值,這個值返回的int類型的 print(val2) #輸出 5 |
3、改寫i.cfg
①刪除section和option
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import?configparser config?=?configparser.ConfigParser() config.read("i.cfg") config.remove_option("section1","k3")??#刪除section1組下的k3 config.remove_section("section2")???#刪除section2組 with?open("i.cfg2","w") as f:???#重新寫入一個文件 ????config.write(f) #輸出,寫入文件的內容 [DEFAULT] k1?=?v1 k2?=?v2 [section1] k4?=?v4 |
②添加section
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import?configparser config?=?configparser.ConfigParser() config.read("i.cfg") sec?=?config.has_option("section2","k5")??#是否存在section2組內有k5 print(sec) #輸出 True sec?=?config.has_section("duoduo")??#是否存在duoduo組 print(sec) #輸出 False config.add_section("duoduo")??#添加section組duoduo config.add_section("duoduo")??#重新寫入到一個配置文件中 with?open("i.cfg3","w") as f: ????config.write(f) |
?③添加或者設置option
| 1 2 3 4 5 6 7 8 9 | import?configparser config?=?configparser.ConfigParser() config.read("i.cfg") config.set("duoduo","z","18")??#設置或者添加duoduo中option值 with?open("i.cfg3","w") as f:???#重新寫入文件中 ????config.write(f) |
?
轉載于:https://www.cnblogs.com/ManyQian/p/8353436.html
總結
以上是生活随笔為你收集整理的小白的Python之路 day5 configparser模块的特点和用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java反射使用及性能比较
- 下一篇: 以太坊基础