Python的configparse模塊

本篇文章給大家分享的是有關(guān)Python的configparse模塊,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

成都創(chuàng)新互聯(lián)公司從2013年開始,我們提供高端網(wǎng)站建設(shè)、小程序開發(fā)、電商視覺設(shè)計(jì)、重慶APP開發(fā)公司及網(wǎng)絡(luò)營(yíng)銷搜索優(yōu)化服務(wù),在傳統(tǒng)互聯(lián)網(wǎng)與移動(dòng)互聯(lián)網(wǎng)發(fā)展的背景下,我們堅(jiān)守著用標(biāo)準(zhǔn)的設(shè)計(jì)方案與技術(shù)開發(fā)實(shí)力作基礎(chǔ),以企業(yè)及品牌的互聯(lián)網(wǎng)商業(yè)目標(biāo)為核心,為客戶打造具商業(yè)價(jià)值與用戶體驗(yàn)的互聯(lián)網(wǎng)+產(chǎn)品。

常用模塊 - configparse模塊

一、簡(jiǎn)介

configparser模塊在Python中是用來讀取配置文件的,配置文件的格式跟windows下的ini配置文件相似,可以包含一個(gè)或多個(gè)節(jié)點(diǎn)(section),每個(gè)節(jié)可以有多個(gè)參數(shù)(鍵=值)。

二、生成配置文件

#! /usr/bin/env python3
# -*- coding:utf-8 -*-
# Note     : 用于測(cè)試configparser模塊的功能
# 導(dǎo)入模塊
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)

運(yùn)行后,文件“example.ini”中的結(jié)果:

[DEFAULT]
compression = yes
compressionlevel = 9
serveraliveinterval = 45
forwardx11 = yes
[bitbucket.org]
user = hg
[topsecret.server.com]
host port = 50022
forwardx11 = no

三、解析配置文件

讀取configparser配置文件的實(shí)例

#! /usr/bin/env python3
# -*- coding:utf-8 -*-
# Note     : 用于測(cè)試configparser模塊的功能
# 導(dǎo)入模塊
import configparser
config = configparser.ConfigParser()
# 讀取配置文件
config.read("example.ini")
print("所有節(jié)點(diǎn)==>", config.sections())
print("包含實(shí)例范圍默認(rèn)值的詞典==>", config.defaults())
for item in config["DEFAULT"]:
    print("循環(huán)節(jié)點(diǎn)topsecret.server.com下所有option==>", item)
print("bitbucket.org節(jié)點(diǎn)下所有option的key,包括默認(rèn)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節(jié)點(diǎn)是否存在==>", 'bitbucket.org' in config)
print("獲取bitbucket.org下user的值==>", config.get("bitbucket.org","user"))
print("獲取option值為數(shù)字的:host port=", config.getint("topsecret.server.com","host port"))

運(yùn)行結(jié)果:

所有節(jié)點(diǎn)==> ['bitbucket.org', 'topsecret.server.com']
包含實(shí)例范圍默認(rèn)值的詞典==> OrderedDict([('compression', 'yes'), ('compressionlevel', '9'), ('serveraliveinterval', 
'45'), ('forwardx11', 'yes')])
循環(huán)節(jié)點(diǎn)topsecret.server.com下所有option==> compression
循環(huán)節(jié)點(diǎn)topsecret.server.com下所有option==> compressionlevel
循環(huán)節(jié)點(diǎn)topsecret.server.com下所有option==> serveraliveinterval
循環(huán)節(jié)點(diǎn)topsecret.server.com下所有option==> forwardx11
bitbucket.org節(jié)點(diǎn)下所有option的key,包括默認(rèn)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節(jié)點(diǎn)是否存在==> True
獲取bitbucket.org下user的值==> hg
獲取option值為數(shù)字的:host port= 50022

刪除配置文件section和option的實(shí)例(默認(rèn)分組有參數(shù)時(shí)無法刪除,但可以先刪除下面的option,再刪分組)

#! /usr/bin/env python3
# -*- coding:utf-8 -*-
# Note     : 用于測(cè)試configparser模塊的功能
# 導(dǎo)入模塊
import configparser
config = configparser.ConfigParser()
# 讀取配置文件
config.read("example.ini")
config.remove_section("bitbucket.org")
"""刪除分組"""
config.remove_option("topsecret.server.com", "host port")
"""刪除某組下面的某個(gè)值"""
config.write(open('example.ini', "w"))

運(yùn)行后,文件“example.ini”中的結(jié)果:

[DEFAULT]
compression = yes
compressionlevel = 9
serveraliveinterval = 45
forwardx11 = yes
[topsecret.server.com]
forwardx11 = no

修改配置文件

#! /usr/bin/env python3
# -*- coding:utf-8 -*-
# Note     : 用于測(cè)試configparser模塊的功能
# 導(dǎo)入模塊
import configparser
config = configparser.ConfigParser()
# 讀取配置文件
config.read("example.ini")
config.add_section("new_section")
"""新增分組"""
config.set("DEFAULT", "compressionlevel", "110")
"""設(shè)置DEFAULT分組下compressionlevel的值為110"""
config.write(open('example.ini', "w"))

運(yùn)行后,文件“example.ini”中的結(jié)果:

[DEFAULT]
compression = yes
compressionlevel = 110
serveraliveinterval = 45
forwardx11 = yes
[topsecret.server.com]
forwardx11 = no
[new_section]

以上就是Python的configparse模塊,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前文章:Python的configparse模塊
本文地址:http://bm7419.com/article16/psccgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)外貿(mào)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、云服務(wù)器品牌網(wǎng)站制作、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)