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}} <class 'dict'>

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}}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试小白00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值