av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

Python ConfigParser模塊的使用示例

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

前言

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

一、ConfigParser模塊簡介

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

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

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

二、ConfigParser模塊使用

1.寫入操作

代碼如下:

import configparser #引入模塊​config = configparser.ConfigParser() #類中一個方法 #實例化一個對象​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) #將對象寫入文件以上代碼做個簡單的解釋,和字典的操作方式相比,configparser模塊的操作方式,無非是在實例化的對象后面,跟一個section,在緊跟著設(shè)置section的屬性(類似字典的形式) config['DEFAULT'] = {’ServerAliveInterval’: ’45’, ’Compression’: ’yes’, ’CompressionLevel’: ’9’, ’ForwardX11’:’yes’ } #類似于操作字典的形式#config后面跟的是一個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會默認(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’下所有鍵值對 [(’serveraliveinterval’, ’45’), (’compression’, ’yes’), (’compressionlevel’, ’9’), (’forwardx11’, ’yes’), (’user’, ’Atlan’)]​print(config.get(’bitbucket.org’,’compression’)) # yes get方法Section下的key對應(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') #刪除一個配置項# 修改某個option的值,如果不存在該option 則會創(chuàng)建config.set(’topsecret.server.com’,’k1’,’11111’)config.set(’yuan’,’k2’,’22222’)#寫回文件config.write(open('example.ini', 'w'))# 寫到其他文件with open(’new2.ini’,’w’) as f: config.write(f)

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 不卡的av电影 | 欧美一区二区三区在线看 | 午夜影院污 | 欧美日韩一区在线 | 天天干天天操 | 国产精品视频在线观看 | 午夜激情国产 | 日韩成人高清 | 黄网址在线观看 | 中文字幕一区二区三区四区 | 日本激情一区二区 | 一级在线视频 | 成人美女免费网站视频 | 99re在线视频精品 | 国产91丝袜在线播放 | 一级片免费观看 | 欧美在线视频一区二区 | 国产日韩欧美一区二区 | 国产精品区二区三区日本 | 成人一区二 | 久草综合在线 | 黑人巨大精品欧美一区二区免费 | 91视视频在线观看入口直接观看 | 亚洲成人综合在线 | 毛片免费看的 | 黑人巨大精品欧美一区二区免费 | 欧美黄a | 337p日本欧洲亚洲大胆精蜜臀 | 国产精品久久久久久久久免费桃花 | 日韩三级在线 | 欧美国产精品一区二区三区 | 欧美精品久久久久久久久老牛影院 | 国产精品揄拍一区二区久久国内亚洲精 | 久久久久久久久久久福利观看 | 亚洲成人免费电影 | 国产激情综合五月久久 | 午夜精品视频在线观看 | 国产精品视屏 | 亚洲精品视频免费观看 | 日韩视频免费看 | 亚洲精品在线免费播放 |