import configparser
專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)江城免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000多家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
config = configparser.ConfigParser()
#在DEFAULT節(jié)點(diǎn)下添加,類(lèi)似字典的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)
#serveraliveinterval = 45
#compression = yes
#compressionlevel = 9
#forwardx11 = yes
#[bitbucket.org]
#alex = 33
#user = hg
#[topsecret.server.com]
#host port = 50022
#forwardx11 = no
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)建新文件寫(xiě)入
#sec = conf.remove_section('bitbucket.org')
#conf .write(open('exmple.cfg', "w"))
#刪除后,覆蓋原文件
#sec = conf.remove_section('bitbucket.org')
#conf .write(open('exmple.ini', "w"))