栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python使用ini&yaml配置文件

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python使用ini&yaml配置文件

一、ini使用 1、语法
[section]
key=value
key1=value
2、注意事项

(1)整个ini文件下,[seciton]不能重复

(2)同一个[seciton]下,key值不能重复

(3)"="等号前后不能有空格

(4)默认读取出来的是字符串类型

(5)增删改的时候,只是对内存里面的数据进行修改,并不会修改实际文件

3、configparser 使用
from configparser import ConfigParser


class GetConfiguration:
    def __init__(self, filename):
        self.conf = ConfigParser()
        self.conf.read(filenames=filename, encoding="utf-8")

    # 添加section
    def insert_section(self, section):
        return self.conf.add_section(section=section)

    # 添加key和value
    def insert_key_value(self, section, option, value):
        self.conf.set(section=section, option=option, value=value)

    # 获取sections名称
    def get_sections1(self):
        return self.conf.sections()

    # 获取sections对象
    def get_sections2(self):
        sections = self.conf.keys()
        for i in sections:
            print(i)

    # 获取指定sections下面的所有的key值
    def get_section_keys(self, section):
        return self.conf.options(section=section)

    # 获取指定section下面的value值--str
    def get_section_value(self, section, option):
        return self.conf.get(section=section, option=option)

    # 获取指定section下面的value值--bool
    def get_section_bool(self, section, option):
        return self.conf.getboolean(section=section, option=option)

    # 获取指定section下面指定的value值--int
    def get_section_int(self, section, option):
        return self.conf.getboolean(section=section, option=option)

    # 获取指定section下面的所有的key和value值
    def get_section_key_value(self, section):
        return self.conf.items(section=section)

    # 删除 options值
    def delete_option(self, section, option):
        return self.conf.remove_option(section=section, option=option)

    # 写入保存,覆盖写入
    # 将你内存你的数据全部写入文件进行覆盖,conf 初始化的时候读取的是所有对象
    def write_section(self, filename):
        self.conf.write(fp=open(file=filename, mode="w"))


if __name__ == '__main__':
    cl = GetConfiguration("test.ini")
    # cl.get_sections2()
    print(cl.get_section_value("mysql", "user"))
    # print(cl.get_section_bool("mysql", "isopen"))
    # print(cl.get_sections1())
    # print(cl.get_section_keys("mysql"))
二、yaml的使用 1、安装pyyaml

pip install pyyaml

2、yaml文件支持的数据类型

(1)字典      mysql: python   冒号后要有空格

(2)列表     - python             冒号后要有空格

(3)纯数字

(4)嵌套---value可以是列表也可以是字典

# value为列表
mysql:
 - root
 - 123456
 - 3306
# value为字典
host:
 name: 1234
# 被读取结果展示:{'mysql': ['root', 123456, 3306], 'host': {'name': 1234, 'age': 20}} 
3、读取yaml文件数据
import yaml
# 使用的第一步:打开文件流
files = open(file="test.yaml",encoding="utf-8")

# 第二步:使用yaml读取数据  stream---文件流    FullLoader---读取文件的方式--全部获取
val_obj = yaml.load(stream=files, Loader=yaml.FullLoader)
print(val_obj, type(val_obj))
files.close()
4、yaml文件的特性

(1)读取出来的python对象可以直接使用

(2)大小敏感(区分大小写)

(3)对外的只有一种数据类型(dict、list)

(4)一次性全部取出来,不支持一个个取值

5、代码演示
import yaml
import os
def handle_yaml(path,name):
    """

    :param path:  文件的绝对路径
    :param name:   .yaml文件名
    :return:
    """
    file_path = os.path.join(path,name)
    with open(file=file_path,encoding="utf-8") as file:
        val = yaml.load(stream=file,Loader=yaml.FullLoader)
        return val


print(handle_yaml(os.path.dirname(os.path.abspath(__file__)),"test.yaml"))
print(handle_yaml(os.path.dirname(__file__),"test.yaml"))
# 输出结果:{'mysql': ['root', 123456, 3306], 'host': {'name': 111}}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/665895.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号