成都网站建设设计

将想法与焦点和您一起共享

configparser模块-创新互联

生成文件

import configparser

创新互联建站是一家以网络技术公司,为中小企业提供网站维护、成都网站制作、网站设计、外贸网站建设、网站备案、服务器租用、空间域名、软件开发、微信小程序开发等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站欢迎来电:18980820575

config = configparser.ConfigParser()
#在DEFAULT节点下添加,类似字典的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节点下添加key为'ForwardX11,value为yes的数据
with open('example.ini', 'w') as configfile:
config.write(configfile)

[DEFAULT] 创建文件的内容

#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())#打印节点(除了“DEFAULT”以外的节点)
print(conf["bitbucket.org"]["user"])
print(conf.defaults())#打印[DEFAULT]

#删除后,创建新文件写入
#sec = conf.remove_section('bitbucket.org')
#conf .write(open('exmple.cfg', "w"))

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:configparser模块-创新互联
当前地址:http://chengdu.cdxwcx.cn/article/dghgpc.html