国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Python ConfigParser模塊的使用示例

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

前言

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

一、ConfigParser模塊簡介

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

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

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

二、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,在緊跟著設置section的屬性(類似字典的形式) config['DEFAULT'] = {’ServerAliveInterval’: ’45’, ’Compression’: ’yes’, ’CompressionLevel’: ’9’, ’ForwardX11’:’yes’ } #類似于操作字典的形式#config后面跟的是一個section的名字,section的段的內容的創建類似于創建字典。類似與字典當然還有別的操作方式啦!config[’bitbucket.org’] = {’User’:’Atlan’} #類似于最經典的字典操作方式

2.讀取操作

import configparserconfig = configparser.ConfigParser()#---------------------------查找文件內容,基于字典的形式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會默認default的鍵 print(key) #user serveraliveinterval compression compressionlevel forwardx11​# 同for循環,找到’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對應的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 則會創建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模塊的使用示例的詳細內容,更多關于Python ConfigParser模塊的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 精品在线99 | 97国产在线视频 | 日本久久久久一级毛片 | 成人性欧美丨区二区三区 | 99精品高清视频一区二区 | 国产成人精品aaaa视频一区 | 生活片一级播放免费 | 114一级毛片免费 | 日韩成人在线播放 | 久久综合给合久久狠狠狠97色69 | 真实国产乱人伦在线视频播放 | 在线综合视频 | 亚洲人成一区二区三区 | 国产精品亚洲精品 | 亚洲精品国产高清不卡在线 | 欧美日本免费观看αv片 | 欧美日韩视频一区二区 | 日韩精品久久久毛片一区二区 | 国产亚洲精品一区二区在线播放 | 一本色道久久88 | 亚洲品质自拍视频网站 | 麻豆一级片 | 欧美一二三区视频 | 午夜免费一级片 | 亚洲精品在线看 | 国产成人香蕉久久久久 | 亚洲专区在线视频 | 久久国产免费 | 美女视频黄a| 精品一区二区三区三区 | 国产亚洲精品看片在线观看 | 国产tv在线 | 亚洲欧美一区二区三区不卡 | 久久久久无码国产精品一区 | 欧美成人影院免费观 | 国产精品亚洲欧美日韩一区在线 | 精品国产无限资源免费观看 | 日韩国产欧美视频 | 看a网站 | 美女视频全部免费 | 国产真实女人一级毛片 |