溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

configparser模塊

發(fā)布時(shí)間:2020-07-11 10:22:48 來(lái)源:網(wǎng)絡(luò) 閱讀:260 作者:堅(jiān)持和學(xué)習(xí) 欄目:編程語(yǔ)言

生成文件

import configparser

config = configparser.ConfigParser()
#在DEFAULT節(jié)點(diǎn)下添加,類似字典的key和value
config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'}

config['bitbucket.org'] = {"alex":33}
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' #在在DEFAULT節(jié)點(diǎn)下添加key為'ForwardX11,value為yes的數(shù)據(jù)
with open('example.ini', 'w') as configfile:
config.write(configfile)

[DEFAULT] 創(chuàng)建文件的內(nèi)容

#serveraliveinterval = 45
#compression = yes
#compressionlevel = 9
#forwardx11 = yes

#[bitbucket.org]
#alex = 33
#user = hg

#[topsecret.server.com]
#host port = 50022
#forwardx11 = no

configparser文件讀取寫入

import configparser

conf=configparser.ConfigParser()
conf.read("example.ini")
print(conf.sections())#打印節(jié)點(diǎn)(除了“DEFAULT”以外的節(jié)點(diǎn))
print(conf["bitbucket.org"]["user"])
print(conf.defaults())#打印[DEFAULT]

#刪除后,創(chuàng)建新文件寫入
#sec = conf.remove_section('bitbucket.org')
#conf .write(open('exmple.cfg', "w"))

#刪除后,覆蓋原文件
#sec = conf.remove_section('bitbucket.org')
#conf .write(open('exmple.ini', "w"))

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI