Python的configparse模塊-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!

成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、做網(wǎng)站、淮陰網(wǎng)絡(luò)推廣、小程序設(shè)計(jì)、淮陰網(wǎng)絡(luò)營銷、淮陰企業(yè)策劃、淮陰品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供淮陰建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn

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

常用模塊 - configparse模塊

一、簡介

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

二、生成配置文件

#! /usr/bin/env python3
# -*- coding:utf-8 -*-
# Note     : 用于測試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     : 用于測試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     : 用于測試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     : 用于測試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)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道。

當(dāng)前標(biāo)題:Python的configparse模塊-創(chuàng)新互聯(lián)
URL鏈接:http://muchs.cn/article28/degjjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、關(guān)鍵詞優(yōu)化手機(jī)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、App開發(fā)、網(wǎng)站排名

廣告

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

微信小程序開發(fā)