日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

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

configparser模塊在python中用來讀取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一個或多個節(jié)點(section), 每個節(jié)可以有多個參數(鍵=值)。使用的配置文件的好處就是不用把程序寫死,可以使程序更靈活。

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

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

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

2、讀取配置文件

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

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

對應輸出

['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、重構封裝

class ReadIni(object):
     # 構造函數
    def __init__(self,file_name=None,node=None):
        '''
        :param file_name:配置文件地址
        :param node: 節(jié)點名
        '''
        #容錯處理
        if file_name == None:
            #默認地址
            file_name = 'D:\liantuo\seleniumTest\config\LocalElement.ini'
        else:
            self.file_name=file_name
        if node == None:
            #默認節(jié)點
            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
#主入口,相當于java的main方法
if __name__ == '__main__':
    #自定義
    # path=r'E:\Pythonx\seleniumTest\config\testIni.ini'   #注意r
    # read_init = ReadIni(file_name=path,node='testa')   #傳入新自定義配置文件地址、節(jié)點
    # print(read_init.get_value('ji'))    #獲取value值
    #默認
    read_init = ReadIni()   #默認配置文件地址、節(jié)點
    print(read_init.get_value('user_name'))  #傳入key值,獲取value

python學習網,免費的在線學習python平臺,歡迎關注!


本文標題:創(chuàng)新互聯(lián)Python教程:python怎么讀取配置文件
轉載源于:http://www.5511xx.com/article/djophpo.html