新聞中心
一、創(chuàng)建配置文件

成都創(chuàng)新互聯(lián)公司是一家專注于做網(wǎng)站、成都網(wǎng)站設(shè)計與策劃設(shè)計,慈溪網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:慈溪等地區(qū)。慈溪做網(wǎng)站價格咨詢:028-86922220
在D盤建立一個配置文件,名字為:test.ini
內(nèi)容如下:
[baseconf] host=127.0.0.1 port=3306 user=root password=root db_name=gloryroad [test] ip=127.0.0.1 int=1 float=1.5 bool=True
注意:要將文件保存為ansi編碼,utf-8編碼會報錯
文件中的[baseconf]為section
二、讀配置文件
import ConfigParser
cf=ConfigParser.ConfigParser()
cf.read(path) 讀配置文件(ini、conf)返回結(jié)果是列表
cf.sections() 獲取讀到的所有sections(域),返回列表類型
cf.options('sectionname') 某個域下的所有key,返回列表類型
cf.items('sectionname') 某個域下的所有key,value對
value=cf.get('sectionname','key') 獲取某個yu下的key對應(yīng)的value值
cf.type(value) 獲取的value值的類型
(1)getint(section, option)
獲取section中option的值,返回int類型數(shù)據(jù),所以該函數(shù)只能讀取int類型的值。
(2)getboolean(section, option)
獲取section中option的值,返回布爾類型數(shù)據(jù),所以該函數(shù)只能讀取boolean類型的值。
(3)getfloat(section, option)
獲取section中option的值,返回浮點類型數(shù)據(jù),所以該函數(shù)只能讀取浮點類型的值。
(4)has_option(section, option)
檢測指定section下是否存在指定的option,如果存在返回True,否則返回False。
(5)has_section(section)
檢測配置文件中是否存在指定的section,如果存在返回True,否則返回False。
三、動態(tài)寫配置文件
cf.add_section('test') 添加一個域
cf.set('test3','key12','value12') 域下添加一個key value對
cf.write(open(path,'w')) 要使用'w'
learn to fail, failure to learn
當(dāng)前文章:創(chuàng)新互聯(lián)Python教程:python的配置文件怎樣寫?
當(dāng)前鏈接:http://www.dlmjj.cn/article/cdheeoj.html


咨詢
建站咨詢
