日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎么讀取配置文件

configparser模塊在python中用來讀取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一個(gè)或多個(gè)節(jié)點(diǎn)(section), 每個(gè)節(jié)可以有多個(gè)參數(shù)(鍵=值)。使用的配置文件的好處就是不用把程序?qū)懰溃梢允钩绦蚋`活。

1、創(chuàng)建配置文件

一般將配置文件創(chuàng)建在config包下,配置文件最好使用.ini格式,示例如下:

[LoginElement]   #節(jié)點(diǎn)(section)
user_name=id>logInName     #其中id決定了通過哪種方式進(jìn)行定位
user_password=id>password
code_image=id>verifyCode
code_text=id>verifyCodeInput
submit=id>submitForm
[mysql]          #節(jié)點(diǎn)(section)
host=id>127.0.0.1
port=id>3306
user=id>root
password=id>123456

2、讀取配置文件

cf=configparser.ConfigParser()   #創(chuàng)建對(duì)象
cf.read('D:\liantuo\seleniumTest\config\LocalElement.ini',encoding='UTF-8')   #讀取配置文件,直接讀取ini文件內(nèi)容
print(cf.sections())         #獲取ini文件內(nèi)所有的section(節(jié)點(diǎn)),以列表形式返回
print(cf.options("LoginElement"))   #獲取指定sections下所有options (key),以列表形式返回
print(cf.items('LoginElement'))     #獲取指定section下所有的鍵值對(duì)(key-value)
print(cf.get('LoginElement','user_name'))   #獲取section中option的值,返回為string類型
getint(section,option)  #返回int類型
getfloat(section, option)  #返回float類型
getboolean(section,option) #返回boolen類型

*注意:讀取配置文件時(shí)參數(shù)添加encoding='UTF-8' ,防止(UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 15: illegal multibyte sequence)

對(duì)應(yīng)輸出

['LoginElement', 'mysql']
['user_name', 'user_password', 'code_image', 'code_text', 'submit']
[('user_name', 'id>logInName'), ('user_password', 'id>password'), ('code_image', 'id>verifyCode'), ('code_text', 
'id>verifyCodeInput'), ('submit', 'id>submitForm')]
id>logInName

3、重構(gòu)封裝

class ReadIni(object):
     # 構(gòu)造函數(shù)
    def __init__(self,file_name=None,node=None):
        '''
        :param file_name:配置文件地址
        :param node: 節(jié)點(diǎn)名
        '''
        #容錯(cuò)處理
        if file_name == None:
            #默認(rèn)地址
            file_name = 'D:\liantuo\seleniumTest\config\LocalElement.ini'
        else:
            self.file_name=file_name
        if node == None:
            #默認(rèn)節(jié)點(diǎn)
            self.node = "LoginElement"
        else:
            self.node = node
        self.cf = self.load_ini(file_name)
    #加載文件
    def load_ini(self,file_name):
        cf = configparser.ConfigParser()
        cf.read(file_name,encoding='utf-8')
        return cf
    #獲取value得值
    def get_value(self,key):
        data = self.cf.get(self.node,key)
        return data
#主入口,相當(dāng)于java的main方法
if __name__ == '__main__':
    #自定義
    # path=r'E:\Pythonx\seleniumTest\config\testIni.ini'   #注意r
    # read_init = ReadIni(file_name=path,node='testa')   #傳入新自定義配置文件地址、節(jié)點(diǎn)
    # print(read_init.get_value('ji'))    #獲取value值
    #默認(rèn)
    read_init = ReadIni()   #默認(rèn)配置文件地址、節(jié)點(diǎn)
    print(read_init.get_value('user_name'))  #傳入key值,獲取value

python學(xué)習(xí)網(wǎng),免費(fèi)的在線學(xué)習(xí)python平臺(tái),歡迎關(guān)注!


當(dāng)前名稱:創(chuàng)新互聯(lián)Python教程:python怎么讀取配置文件
URL分享:http://www.dlmjj.cn/article/djophpo.html