目录
1. YAML简介2. 语法规则3. 文件数据结构4. YAML数据格式示例5. 安装yaml库6. 读取YAML6.1 读取键值对或嵌套键值对6.2 读取数组类型6.3 多文档同在一份yaml文件中时的读取方法6.4 向YAML文档写入6.5 更新/修改 YAML文件内容7. 使用ruamel模块将数据转换为标准的yaml内容1. YAML简介
YAML是可读性高,用来表达数据序列化格式的,专用于写配置文件的语言。YAML文件其实也是一种配置文件类型,后缀名是.yaml或.yml都可以。其以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁。
(资料图)
2. 语法规则
大小写敏感使用缩进表示层级关系使用空格键缩进,而非Tab键缩进缩进的空格数目不重要,只需要相同层级的元素左侧对齐文件中的字符串不需要使用引号标注,但若字符串包含有特殊字符则需用引号标注注释标识为 #3. 文件数据结构
对象:键值对的集合(简称"映射或字典")
键值对用冒号 “:” 结构表示 冒号与值之间需用空格分隔
数组:一组按序排列的值(简称"序列或列表")
数组前加有 “-” 符号 符号与值之间需用空格分隔
纯量(scalars):单个的、不可再份的值(如:字符串、bool值、整数、浮点数、时间、日期、null等)
None值可用null,也可用~
表示
4. YAML数据格式示例
# 对象:yaml键值对;即Python中字典 user: "admin" pwd: "admin@123" site: "www.yaml.com" # 解析后: {"user": "admin", "password": "admin@123", "site": "www.yaml.com"} # 2. 数组:yaml键值对中嵌套数组 user2: - a - b - c user3: - d # 解析后:{"user2":["a","b","c"],"user3":["d"]} # 3. 纯量 val_name: name # 字符串: {"val_name": "name"} spec_val: "name\n" # 特殊字符串: {"spec_val": "name\n"} pi_val: 3.14 # 数字: {"pi_val": 3.14} bol_val: true # 布尔值: {"bol_val": true} nul_val: null # null值: {"nul_val": None} nul_val: ~ # null值: {"nul_val": None} time_val: 2023-02-03t22:33:22.33-03:00 # 时间值:{"time_val": datetime.datetime(2023, 2, 3, 22, 33, 22, 330000)} date_val: 2024-01-01 # 日期值:{"date_val": datetime.date(2024, 1, 1)} # 4. 引用 name: &name 白云 tester: *name # 相当于 name: 白云 tester: 白云 # 解析后内容:{"name": "白云", "tester": "白云"} # 5. 强制转换 str: !!str 3.14 int: !!int "666" # 输出: {"str": "3.14","int": 123}
5. 安装yaml库
pip install pyyaml
6. 读取YAML
6.1 读取键值对或嵌套键值对
yaml文件内容为:
user1: name: xm stu: 101 user2: name: xh stu: 102 user3: name: xl stu: 103
程序代码:
import yaml import os class ReadYAML(object): def read_yaml(self,yaml_file): with open(yaml_file,"r",encoding="utf-8") as f: file_data = f.read() print("file_data类型:",type(file_data)) data = yaml.safe_load(file_data) print("data类型:",type(data)) return data if __name__ == "__main__": base_name = os.path.dirname(os.path.realpath(__file__)) yaml_path = os.path.join(base_name,"test.yaml") ry = ReadYAML() res = ry.read_yaml(yaml_path) print(res)
输出结果:
file_data类型:
data类型:
{"user1": {"name": "xm", "stu": 101}, "user2": {"name": "xh", "stu": 102}, "user3": {"name": "xl", "stu": 103}}
6.2 读取数组类型
yaml文件内容为:
class1: - stu1 - stu2 - stu3 class2: - stu2
程序代码:
import yaml import os class ReadArraysYAML(object): def read_yaml(self,yaml_file): with open(yaml_file,"r",encoding="utf-8") as f: file_data = f.read() # print("file_data类型:",type(file_data)) data = yaml.safe_load(file_data) # print("data类型:",type(data)) return data if __name__ == "__main__": base_name = os.path.dirname(os.path.realpath(__file__)) yaml_path = os.path.join(base_name,"arrays.yaml") ry = ReadArraysYAML() res = ry.read_yaml(yaml_path) print(res)
输出结果:
{"class1": ["stu1", "stu2", "stu3"], "class2": ["stu2"]}
6.3 多文档同在一份yaml文件中时的读取方法
yaml文件内容:
# 分段yaml文件中存在多个文档 --- animal1: dog age: 1 --- animal2: cat age: 2
程序代码:
""" 多文档同在一份yaml文件中时的读取方法(使用yaml.safe_load_all()) """ import yaml import os def get_yaml_load_all(yaml_file): file = open(yaml_file,"r",encoding="utf-8") file_data = file.read() file.close() all_data = yaml.safe_load_all(file_data) for data in all_data: print(data) if __name__ == "__main__": current_path = os.path.dirname(__file__) print(current_path) yaml_path = os.path.join(current_path,"muti.yaml") get_yaml_load_all(yaml_path)
输出结果:
d:\PyProject\YAML
{"animal1": "dog", "age": 1}
{"animal2": "cat", "age": 2}
6.4 向YAML文档写入
程序代码:
""" 使用yaml.dump()方法将列表或字典数据写入进已存在的yaml文档 """ import yaml import os def generate_yaml_doc(yaml_file): py_object = {"school":"Fxxking U","student":["stu1","stu2"]} file = open(yaml_file,"w",encoding="utf-8") yaml.safe_dump(py_object,file) file.close() if __name__ == "__main__": current_path = os.path.dirname(__file__) print(current_path) yaml_path = os.path.join(current_path,"generateYAML.yaml") generate_yaml_doc(yaml_path)
写入后,YAML文档内容:
school: Fxxking U student: - stu1 - stu2
注:若想要以追加的形式写入,只需将open()中的’w’改为’a’即可
6.5 更新/修改 YAML文件内容
修改前YAML文件内容:
school: Fxxking U student: - stu1 - stu2
程序代码:
import yaml import os from readArraysYAML import ReadArraysYAML def update_yaml(k,v,yaml_file): readY = ReadArraysYAML() old_data = readY.read_yaml(yaml_file) old_data[k] = v # 修改读取的数据,如果k不存在则新增一组键值对 with open(yaml_file,"w",encoding="utf-8") as f: yaml.safe_dump(old_data,f) if __name__ == "__main__": current_path = os.path.dirname(__file__) yaml_path = os.path.join(current_path,"generateYAML.yaml") k = "school" v = "SZ U" update_yaml(k,v,yaml_path)
修改后,YAML文件内容:
school: SZ U student: - stu1 - stu2
7. 使用ruamel模块将数据转换为标准的yaml内容
安装ruamel库
pip install ruamel.yaml
程序代码:
from ruamel import yaml import os def generate_yaml_doc_ruamel(yaml_file): py_object = {"file_type":"ruamel_yaml","school":"Fxxking U","student":["c","d"]} with open(yaml_file,"w",encoding="utf-8") as f: yaml.dump(py_object,f,Dumper=yaml.RoundTripDumper) if __name__ == "__main__": current_path = os.path.dirname(__file__) yaml_path = os.path.join(current_path,"ruamelGenerateYAML.yaml") generate_yaml_doc_ruamel(yaml_path) print("写入成功!")
写入后,YAML文件内容:
file_type: ruamel_yaml school: Fxxking U student: - c - d
到此这篇关于Python使用yaml模块操作YAML文档的文章就介绍到这了,更多相关Python使用yaml模块内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?