zoukankan      html  css  js  c++  java
  • 配置文件的操作和封装

    配置文件

    常用的配置文件格式.ini/.conf

    配置文件的创建(conf.ini)

    [logging]
    level = DEBUG
    f_level = DEBUG
    s_level = ERROR
    
    [desire]
    name = desire
    age = 18
    sex = 男
    money = 1000.99
    switch = True
    

    配置文件的操作

    1、导包
    from configparser import ConfigParser
    
    2、创建一个操作配置文件的对象(文件解析对象)
    conf = ConfigParser()
    
    3、读取配置文件中的内容
    conf.read("conf.ini",encoding="utf8")
    
    get方法:读取出来的内容都是字符串
    res1 = conf.get("logging", "level")
    
    getint:读取整数类型的数据,读取出来是int类型
    res3 = conf.getint("desire","age")
    
    getfloat:读取浮点数
    res4 = conf.getfloat("desire", "money")
    
    getboolean:读取布尔值
    res5 = conf.getboolean("desire", "switch")
    
    4、配置数据的写入
    conf.set("desire", "bbb", "python999")
    conf.write(open("conf.ini", "w",encoding="utf-8"))
    

    封装配置文件类

    class MyConf(ConfigParser):
        """封装的读取配置文件类"""
    
        def __init__(self, file_name, encoding='utf8'):
            """
            初始化
            :param file_name: 配置文件名
            :param encoding: 编码格式
            """
            super().__init__()
            self.file_name = file_name
            self.encoding = encoding
            self.read(file_name, encoding=encoding)
    
        def write_data(self, section, option, value):
            """
            添加值
            :param section: 配置块
            :param option: 配置属性
            :param value: 对应的配置属性值
            """
            self.set(section, option, value)
            self.write(open(self.file_name, 'w', encoding=self.encoding))
    
  • 相关阅读:
    用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
    用sqoop将mysql的数据导入到hive表中
    windows下启动mysql服务的命令行启动和手动启动方法
    使sqoop能够启用压缩的一些配置
    简易安装sqoop
    sqoop一些语法的使用
    mysql 视图
    MySQL 函数大全及用法示例
    MySQL 触发器 -1
    MySQL 函数
  • 原文地址:https://www.cnblogs.com/desireyang/p/12055385.html
Copyright © 2011-2022 走看看