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

首頁(yè) > 編程 > Python > 正文

Python內(nèi)置模塊ConfigParser實(shí)現(xiàn)配置讀寫(xiě)功能的方法

2020-01-04 15:51:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Python內(nèi)置模塊ConfigParser實(shí)現(xiàn)配置讀寫(xiě)功能的方法。分享給大家供大家參考,具體如下:

用于對(duì)特定的配置進(jìn)行操作,當(dāng)前模塊的名稱在 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的鍵值對(duì)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"))#設(shè)置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"))

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 陇川县| 颍上县| 泰宁县| 辛集市| 龙里县| 临洮县| 蒲城县| 宣恩县| 自治县| 长葛市| 大名县| 光泽县| 普兰县| 峨眉山市| 化隆| 九寨沟县| 水城县| 红原县| 太谷县| 洪泽县| 绥芬河市| 铜鼓县| 祁连县| 疏勒县| 灵台县| 长沙县| 文安县| 赤壁市| 华亭县| 疏勒县| 蒙阴县| 噶尔县| 陆河县| 民丰县| 营口市| 六安市| 连州市| 芒康县| 和静县| 车致| 阜城县|