国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python內置模塊ConfigParser實現配置讀寫功能的方法

2020-02-22 23:15:10
字體:
來源:轉載
供稿:網友

本文實例講述了Python內置模塊ConfigParser實現配置讀寫功能的方法。分享給大家供大家參考,具體如下:

用于對特定的配置進行操作,當前模塊的名稱在 python 3.x 版本中變更為 configparser。

#配置文件test.cfg[section1]k1 = v1k2 :v2k3 = 1[section2]k1 = v1
#coding:utf-8import ConfigParserconfig = ConfigParser.ConfigParser()config.read('test.cfg')# ########## 讀 ###########獲取所有sections.secs = config.sections()print secs #['section1', 'section2']#獲取制定section的鍵keyoptions = config.options('section1')print options  #['k1', 'k2', 'k3']#獲取指定section的鍵值對key-valueitem_list = config.items('section1')print item_list #[('k1', 'v1'), ('k2', 'v2'), ('k3', '1')]#獲取指定key的value# 獲取字符串類型的valueval1 = config.get('section1','k1')# 獲取整型的valueval2 = config.getint('section1','k3')# ########## 增改刪 ########### 增加sectionif not config.has_section('section3'):  config.add_section('section3')  config.write(open('test.cfg', "w"))#設置optionif not config.has_section('section3'):  config.set('section3','k1',11111)  config.write(open('test.cfg', "w"))# 移除optionret = config.remove_option('section3','k1')print ret  #True or Falseconfig.write(open('test.cfg', "w"))# 移除sectionret = config.remove_section('section3')print ret  #True or Falseconfig.write(open('test.cfg', "w"))

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沅陵县| 会泽县| 灌云县| 道真| 交城县| 榆社县| 邵武市| 宁化县| 郑州市| 噶尔县| 静海县| 海林市| 信阳市| 亳州市| 开封市| 买车| 安塞县| 连州市| 惠安县| 木兰县| 开化县| 工布江达县| 朔州市| 县级市| 习水县| 卢龙县| 绥化市| 双峰县| 西和县| 玉田县| 乐都县| 普格县| 徐闻县| 浮梁县| 康平县| 延长县| 南华县| 梅河口市| 阳江市| 宜昌市| 金阳县|