python读取配置文件的方法-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

目前成都创新互联已为上千余家的企业提供了网站建设、域名、雅安服务器托管绵阳服务器托管、企业网站设计、雁峰网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

这篇文章主要介绍python读取配置文件的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

configparser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节点(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用把程序写死,可以使程序更灵活。

1、创建配置文件

一般将配置文件创建在config包下,配置文件最好使用.ini格式,示例如下:

[LoginElement]   #节点(section)
user_name=id>logInName     #其中id决定了通过哪种方式进行定位
user_password=id>password
code_image=id>verifyCode
code_text=id>verifyCodeInput
submit=id>submitForm
[mysql]          #节点(section)
host=id>127.0.0.1
port=id>3306
user=id>root
password=id>123456

2、读取配置文件

cf=configparser.ConfigParser()   #创建对象
cf.read('D:\liantuo\seleniumTest\config\LocalElement.ini',encoding='UTF-8')   #读取配置文件,直接读取ini文件内容
print(cf.sections())         #获取ini文件内所有的section(节点),以列表形式返回
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: 节点名
        '''
        #容错处理
        if file_name == None:
            #默认地址
            file_name = 'D:\liantuo\seleniumTest\config\LocalElement.ini'
        else:
            self.file_name=file_name
        if node == None:
            #默认节点
            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')   #传入新自定义配置文件地址、节点
    # print(read_init.get_value('ji'))    #获取value值
    #默认
    read_init = ReadIni()   #默认配置文件地址、节点
    print(read_init.get_value('user_name'))  #传入key值,获取value

以上是python读取配置文件的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联-成都网站建设公司行业资讯频道!

当前名称:python读取配置文件的方法-创新互联
分享链接:https://www.cdcxhl.com/article22/csdijc.html

成都网站建设公司_创新互联,为您提供面包屑导航动态网站标签优化服务器托管网站策划搜索引擎优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设