如何实现Python中ini配置文件读写操作
这篇文章将为大家详细讲解有关如何实现Python中ini配置文件读写操作,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
导入模块importconfigparser#py3写入
config=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['HostPort']='50022'#mutatestheparsertopsecret['ForwardX11']='no'#samehereconfig['DEFAULT']['ForwardX11']='yes'#写入文件withopen('example.ini','w')asconfigfile:config.write(configfile)读取
config=configparser.ConfigParser()config.read("example.ini")print(config.defaults())#OrderedDict([('compression','yes')])print(config.sections())#['bitbucket.org','topsecret.server.com']print(config['bitbucket.org']['User'])#hgprint(config.options("topsecret.server.com"))#['port','compression']print(config.items("topsecret.server.com"))#[('compression','yes'),('port','50022')]print(config.get("topsecret.server.com","port"))#50022修改
print(config.has_section("Name"))#删除config.remove_section("Name")#添加config.add_section("Name")config["Name"]["name"]="Tom"config["Name"]["asname"]="Jimi"#设置config.remove_option("Name","asname")config.set("Name","name","Jack")#保存config.write(open("example.ini","w"))附:ini文件
[DEFAULT]serveraliveinterval=45compression=yescompressionlevel=9forwardx11=yes[bitbucket.org]user=hg[topsecret.server.com]hostport=50022forwardx11=no
help(configparser)
"""CLASSESclassConfigParser(RawConfigParser)|ConfigParserimplementinginterpolation.||add_section(self,section)|Createanewsectionintheconfiguration.Extends|RawConfigParser.add_sectionbyvalidatingifthesectionnameis|astring.||set(self,section,option,value=None)|Setanoption.ExtendsRawConfigParser.setbyvalidatingtypeand|interpolationsyntaxonthevalue.||defaults(self)||get(self,section,option,*,raw=False,vars=None,fallback=<objectobjectat0x0000000002F42120>)|Getanoptionvalueforagivensection.||getboolean(self,section,option,*,raw=False,vars=None,fallback=<objectobjectat0x0000000002F42120>)||getfloat(self,section,option,*,raw=False,vars=None,fallback=<objectobjectat0x0000000002F42120>)||getint(self,section,option,*,raw=False,vars=None,fallback=<objectobjectat0x0000000002F42120>)||has_option(self,section,option)|Checkfortheexistenceofagivenoptioninagivensection.|Ifthespecified`section'isNoneoranemptystring,DEFAULTis|assumed.Ifthespecified`section'doesnotexist,returnsFalse.||has_section(self,section)|Indicatewhetherthenamedsectionispresentintheconfiguration.|items(self,section=<objectobjectat0x0000000002F42120>,raw=False,vars=None)|Returnalistof(name,value)tuplesforeachoptioninasection.||options(self,section)|Returnalistofoptionnamesforthegivensectionname.|popitem(self)|Removeasectionfromtheparserandreturnitas|read(self,filenames,encoding=None)|Readandparseafilenameoralistoffilenames.|Returnlistofsuccessfullyreadfiles.||read_dict(self,dictionary,source='<dict>')|Readconfigurationfromadictionary.||read_file(self,f,source=None)|Likeread()buttheargumentmustbeafile-likeobject.||read_string(self,string,source='<string>')|Readconfigurationfromagivenstring.||readfp(self,fp,filename=None)|Deprecated,useread_fileinstead.||remove_option(self,section,option)|Removeanoption.||remove_section(self,section)|Removeafilesection.||sections(self)|Returnalistofsectionnames,excluding[DEFAULT]||write(self,fp,space_around_delimiters=True)|Writean.ini-formatrepresentationoftheconfigurationstate.||clear(self)|D.clear()->None.RemoveallitemsfromD.||pop(self,key,default=<objectobjectat0x0000000002F42040>)|D.pop(k[,d])->v,removespecifiedkeyandreturnthecorrespondingvalue.|Ifkeyisnotfound,disreturnedifgiven,otherwiseKeyErrorisraised.||setdefault(self,key,default=None)|D.setdefault(k[,d])->D.get(k,d),alsosetD[k]=difknotinD||update(*args,**kwds)|D.update([E,]**F)->None.UpdateDfrommapping/iterableEandF.|IfEpresentandhasa.keys()method,does:forkinE:D[k]=E[k]|IfEpresentandlacks.keys()method,does:for(k,v)inE:D[k]=v|Ineithercase,thisisfollowedby:fork,vinF.items():D[k]=v||keys(self)|D.keys()->aset-likeobjectprovidingaviewonD'skeys||values(self)|D.values()->anobjectprovidingaviewonD'svalues|"""
关于“如何实现Python中ini配置文件读写操作”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。