configparser模塊在Python中是用來讀取配置文件的,配置文件的格式跟windows下的ini配置文件類似,能夠包含一個或多個節點(section),每一個節能夠有多個參數(鍵=值)。python
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : mayi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2019/4/3 # Name : test01 # Software : PyCharm # Note : 用於測試configparser模塊的功能 # 導入模塊 import configparser config = configparser.ConfigParser() """生成configparser配置文件 ,字典的形式""" """第一種寫法""" config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} """第二種寫法""" config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'hg' """第三種寫法""" config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Host Port'] = '50022' # mutates the parser topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes' """寫入後綴爲.ini的文件""" with open('example.ini', 'w') as configfile: config.write(configfile)
運行後,文件「example.ini」中的結果:windows
[DEFAULT] compression = yes compressionlevel = 9 serveraliveinterval = 45 forwardx11 = yes [bitbucket.org] user = hg [topsecret.server.com] host port = 50022 forwardx11 = no
讀取configparser配置文件的實例測試
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : mayi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2019/4/3 # Name : test01 # Software : PyCharm # Note : 用於測試configparser模塊的功能 # 導入模塊 import configparser config = configparser.ConfigParser() # 讀取配置文件 config.read("example.ini") print("全部節點==>", config.sections()) print("包含實例範圍默認值的詞典==>", config.defaults()) for item in config["DEFAULT"]: print("循環節點topsecret.server.com下全部option==>", item) print("bitbucket.org節點下全部option的key,包括默認option==>", config.options("bitbucket.org")) print("輸出元組,包括option的key和value", config.items('bitbucket.org')) print("bitbucket.org下user的值==>", config["bitbucket.org"]["user"]) # 方式一 topsecret = config['bitbucket.org'] print("bitbucket.org下user的值==>", topsecret["user"]) # 方式二 print("判斷bitbucket.org節點是否存在==>", 'bitbucket.org' in config) print("獲取bitbucket.org下user的值==>", config.get("bitbucket.org","user")) print("獲取option值爲數字的:host port=", config.getint("topsecret.server.com","host port"))
運行結果:spa
全部節點==> ['bitbucket.org', 'topsecret.server.com'] 包含實例範圍默認值的詞典==> OrderedDict([('compression', 'yes'), ('compressionlevel', '9'), ('serveraliveinterval', '45'), ('forwardx11', 'yes')]) 循環節點topsecret.server.com下全部option==> compression 循環節點topsecret.server.com下全部option==> compressionlevel 循環節點topsecret.server.com下全部option==> serveraliveinterval 循環節點topsecret.server.com下全部option==> forwardx11 bitbucket.org節點下全部option的key,包括默認option==> ['user', 'compression', 'compressionlevel', 'serveraliveinterval', 'forwardx11'] 輸出元組,包括option的key和value [('compression', 'yes'), ('compressionlevel', '9'), ('serveraliveinterval', '45'), ('forwardx11', 'yes'), ('user', 'hg')] bitbucket.org下user的值==> hg bitbucket.org下user的值==> hg 判斷bitbucket.org節點是否存在==> True 獲取bitbucket.org下user的值==> hg 獲取option值爲數字的:host port= 50022
刪除配置文件section和option的實例(默認分組有參數時沒法刪除,但能夠先刪除下面的option,再刪分組)code
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : mayi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2019/4/3 # Name : test01 # Software : PyCharm # Note : 用於測試configparser模塊的功能 # 導入模塊 import configparser config = configparser.ConfigParser() # 讀取配置文件 config.read("example.ini") config.remove_section("bitbucket.org") """刪除分組""" config.remove_option("topsecret.server.com", "host port") """刪除某組下面的某個值""" config.write(open('example.ini', "w"))
運行後,文件「example.ini」中的結果:server
[DEFAULT] compression = yes compressionlevel = 9 serveraliveinterval = 45 forwardx11 = yes [topsecret.server.com] forwardx11 = no
修改配置文件blog
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : mayi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2019/4/3 # Name : test01 # Software : PyCharm # Note : 用於測試configparser模塊的功能 # 導入模塊 import configparser config = configparser.ConfigParser() # 讀取配置文件 config.read("example.ini") config.add_section("new_section") """新增分組""" config.set("DEFAULT", "compressionlevel", "110") """設置DEFAULT分組下compressionlevel的值爲110""" config.write(open('example.ini', "w"))
運行後,文件「example.ini」中的結果:utf-8
[DEFAULT] compression = yes compressionlevel = 110 serveraliveinterval = 45 forwardx11 = yes [topsecret.server.com] forwardx11 = no [new_section]