国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python ConfigParser模塊的使用示例

瀏覽:5日期:2022-07-08 15:17:58

前言

在做項(xiàng)目的時(shí)候一些配置文件都會(huì)寫(xiě)在settings配置文件中,今天在研究'州的先生'開(kāi)源文檔寫(xiě)作系統(tǒng)-MrDoc的時(shí)候,發(fā)現(xiàn)部分配置文件寫(xiě)在config.ini中,并利用configparser進(jìn)行相關(guān)配置文件的讀取及修改。

一、ConfigParser模塊簡(jiǎn)介

該模塊適用于配置文件的格式與windows ini文件類似,是用來(lái)讀取配置文件的包。配置文件的格式如下:中括號(hào)“[ ]”內(nèi)包含的為section。section 下面為類似于key-value 的配置內(nèi)容。格式如下:

[DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9ForwardX11 = yes[bitbucket.org]User = Atlan[topsecret.server.com]Port = 50022ForwardX11 = no

括號(hào)“[ ]”內(nèi)包含的為section。緊接著section 為類似于key-value 的options 的配置內(nèi)容。

二、ConfigParser模塊使用

1.寫(xiě)入操作

代碼如下:

import configparser #引入模塊​config = configparser.ConfigParser() #類中一個(gè)方法 #實(shí)例化一個(gè)對(duì)象​config['DEFAULT'] = {’ServerAliveInterval’: ’45’, ’Compression’: ’yes’, ’CompressionLevel’: ’9’, ’ForwardX11’:’yes’ } #類似于操作字典的形式​config[’bitbucket.org’] = {’User’:’Atlan’} #類似于操作字典的形式​config[’topsecret.server.com’] = {’Host Port’:’50022’,’ForwardX11’:’no’}​with open(’example.ini’, ’w’) as configfile:​ config.write(configfile) #將對(duì)象寫(xiě)入文件以上代碼做個(gè)簡(jiǎn)單的解釋,和字典的操作方式相比,configparser模塊的操作方式,無(wú)非是在實(shí)例化的對(duì)象后面,跟一個(gè)section,在緊跟著設(shè)置section的屬性(類似字典的形式) config['DEFAULT'] = {’ServerAliveInterval’: ’45’, ’Compression’: ’yes’, ’CompressionLevel’: ’9’, ’ForwardX11’:’yes’ } #類似于操作字典的形式#config后面跟的是一個(gè)section的名字,section的段的內(nèi)容的創(chuàng)建類似于創(chuàng)建字典。類似與字典當(dāng)然還有別的操作方式啦!config[’bitbucket.org’] = {’User’:’Atlan’} #類似于最經(jīng)典的字典操作方式

2.讀取操作

import configparserconfig = configparser.ConfigParser()#---------------------------查找文件內(nèi)容,基于字典的形式print(config.sections()) # []config.read(’example.ini’,encoding=’utf-8’)print(config.sections()) # [’bitbucket.org’, ’topsecret.server.com’]print(’bytebong.com’ in config) # Falseprint(’bitbucket.org’ in config) # Trueprint(’DEFAULT’ in config) # Trueprint(config[’bitbucket.org’]['user']) # Atlan​print(config[’DEFAULT’][’Compression’]) #yesprint(config[’topsecret.server.com’][’ForwardX11’]) #no​print(config[’bitbucket.org’]) #<Section: bitbucket.org>for key in config[’bitbucket.org’]: # 注意,有default會(huì)默認(rèn)default的鍵 print(key) #user serveraliveinterval compression compressionlevel forwardx11​# 同for循環(huán),找到’bitbucket.org’下所有鍵 [’user’, ’serveraliveinterval’, ’compression’, ’compressionlevel’, ’forwardx11’]print(config.options(’bitbucket.org’)) ​print(config.items(’bitbucket.org’)) #找到’bitbucket.org’下所有鍵值對(duì) [(’serveraliveinterval’, ’45’), (’compression’, ’yes’), (’compressionlevel’, ’9’), (’forwardx11’, ’yes’), (’user’, ’Atlan’)]​print(config.get(’bitbucket.org’,’compression’)) # yes get方法Section下的key對(duì)應(yīng)的valueprint(config.getboolean(’bitbucket.org’,’compression’)) # True

3.修改操作

import configparser​config = configparser.ConfigParser()​config.read(’example.ini’,encoding=’utf-8’) #讀文件​config.add_section(’yuan’) #添加section​config.remove_section(’bitbucket.org’) #刪除sectionconfig.remove_option(’topsecret.server.com’,'forwardx11') #刪除一個(gè)配置項(xiàng)# 修改某個(gè)option的值,如果不存在該option 則會(huì)創(chuàng)建config.set(’topsecret.server.com’,’k1’,’11111’)config.set(’yuan’,’k2’,’22222’)#寫(xiě)回文件config.write(open('example.ini', 'w'))# 寫(xiě)到其他文件with open(’new2.ini’,’w’) as f: config.write(f)

以上就是Python ConfigParser模塊的使用示例的詳細(xì)內(nèi)容,更多關(guān)于Python ConfigParser模塊的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 佳木斯市| 隆林| 鹤庆县| 罗甸县| 延边| 崇文区| 察隅县| 华宁县| 崇文区| 罗源县| 南华县| 桑日县| 乐亭县| 龙游县| 岗巴县| 武山县| 吴堡县| 长宁县| 大同市| 五原县| 满城县| 饶河县| 柳林县| 安新县| 保康县| 新巴尔虎右旗| 孝感市| 酉阳| 宝山区| 马公市| 肥西县| 南投市| 开鲁县| 寿宁县| 通江县| 化州市| 雅江县| 吉首市| 乐陵市| 樟树市| 什邡市|