python自动化笔记:配置文件.ini及yml文件

一、.ini配置文件

后缀名.ini 用于存储项目全局配置变量
比如:接口地址 项目地址…输出文件路径

1.1、ini编写格式

[节点]
选项=选项值
注释前面加;
注意:节点不可以重复

[section]
option=value
option=value

创建nmb.ini文件,代码如下:

[log]
name = py30
level = INFO
file_ok = TRUE
file_name = py30.log

[mysql]
user = nmb
passwd = 123456

1.2、读取.ini配置文件的数据

from configparser import ConfigParser

# 实例化
config = ConfigParser()
# 读取配置文件
config.read("nmb.ini",encoding="utf-8")
# 获取ini文件中所有节点
sections = config.sections()
print(sections)
# 获取ini文件中某个节点下所有选项
options = config.options(section="log")
print(options)
# 获取ini文件中某个节点下某个选项的选项值,默认读取到的全部都是字符串
value = config.get(section="log",option="file_ok")
print(value)
print(type(value))
# 使读取出来为布尔值
val = config.getboolean(section="log",option="file_ok")
print(val)
# 获取ini文件中某个节点下所有选项及选项值---》元组列表
values = config.items(section="log")
print(values)

1.3、编辑:写入和删除(了解即可)

# 写入一个节点
new_section="userinfo"
if new_section not in sections:
    config.add_section(new_section)
    # 给某个节点添加选项及选项值
    config.set(section=new_section, option="username", value="luban")
    config.set(section=new_section, option="passwd", value="123456")
    with open("nmb.ini", "w+", encoding="utf-8") as file:
        config.write(file)
else:
    # 写入
    config.set("log", "file_name", "py30303.log")
    # 写入文件
    with open("nmb.ini", "w", encoding="utf-8") as file:
        config.write(file)
    # 另一种写法 config.write(open("nmb.ini", "w", encoding="utf-8"))
 
 # 删除节点
del_section = "userinfo"
print(sections)
if del_section in sections:
    config.remove_section(del_section)
    with open("nmb.ini","w+") as file:
        config.write(file)

# 删除选项及选项值
del_section1 = "userinfo1"
del_option = "username1"
config.remove_option(section=del_section1, option=del_option)
with open("nmb.ini", "w+") as file:
    config.write(file)

二、yaml文件

2.1、yaml编写语法规则

1、大小写敏感
2、使用缩进表示层级关系
3、禁止使用TAB缩进,缩进只能用空格,相同的层级元素左对齐即可
4、使用#表示注释
5、字符串可以不用引号标注

2.2、yaml三种数据结构

1、字典
使用冒号(:)表示键值对,同一缩进的所有键值对属于一个map

# Yaml(注意冒号后的空格)
name: luban
age: 15

2、列表
使用连字符(-)表示,注意-后的空格

- hello
- world

3、scalar(纯量)
字符串、数字、布尔值

2.3、yaml文件的读取和写入

先安装第三方库 PyYAML

1、读取

import yaml

with open("config.yml", "r") as file:
    data = yaml.load(stream=file, Loader=yaml.FullLoader)
    print(data)

2、写入

import yaml
modules=["中文", "pytest", "unittest", "requests", "requests"]
with open("config.yml", "a+") as file:
    yaml.dump(data=modules, stream=file, allow_unicode=True, encoding="utf-8")

在这里插入图片描述

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python配置文件通常使用YAML (.yml)格式。YAML是一种人类可读的数据序列化格式,用于表示数据结构和配置。在Python中,可以使用yaml库来解析和读取YAML格式的配置文件。以下是一个示例代码片段,展示了如何读取一个YAML配置文件并打印其中的内容: ```python import os import yaml # 获取yaml文件路径 yamlPath = os.path.join("D:\\test\\", "config.yml") # 打开并读取yaml文件 with open(yamlPath, 'r', encoding='utf-8') as f: cfg = yaml.safe_load(f) # 读取配置文件中的内容 user_name = cfg['user_name'] plan_date = cfg['date'] if 'date' in cfg else '' user_name_list = cfg['user_name_list'] # 打印配置文件中的内容 print(user_name) print(plan_date) for element in user_name_list: print(element.get('user_name')) ``` 以上代码使用yaml库的`safe_load`方法来加载YAML配置文件,并将其转换为Python字典对象。然后,可以通过字典的键来访问配置文件中的不同配置项。在这个例子中,我们将`user_name`、`date`和`user_name_list`的值打印出来。 请注意,你需要根据实际的配置文件路径和键名进行相应的修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python解析yml配置文件](https://blog.csdn.net/godloveleo9527/article/details/123046694)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值