python使用yaml格式文件的方法

Liana ·
更新时间:2024-09-20
· 795 次阅读

目录

安装PyYAML安装包

配置yaml文件

编写yaml文件内容

python读取yaml文件

yaml文件与python代码书写比较

字典嵌套字典

列表(或叫数组)中嵌套字典,字典中一组数据

列表(或叫数组)中嵌套字典,字典中多组数据

字典嵌套列表

组合使用

其他类型

安装PyYAML安装包

YAML在python语言中有PyYAML安装包,下载地址:https://pypi.python.org/pypi/PyYAML

联网的windows可直接win+r cmd进入终端pip install PyYAML

配置yaml文件

进入相应python文件的目录,创建yaml文件(略)

通过编程软件创建

自定义文件后缀为 .yaml

编写yaml文件内容 city: shanghai time: 202210 successfully: 200 exception: 500 python读取yaml文件 #使用utf-8编码 # -*- coding:utf-8 -*- #导入yaml模块 import yaml f = open('test.yaml', 'r', encoding='utf-8') #读yaml文件,编码用utf-8 cfg = f.read() #读全部文件 d = yaml.load(cfg, Loader=yaml.FullLoader) # 用load方法把读到的yaml文件内容转换成字典类型 # yaml5.1版本后弃用了yaml.load(file)这个用法,因为觉得很不安全,5.1版本之后就修改了需要指定Loader,通过默认加载器(FullLoader)禁止执行任意函数 # Loader=yaml.FullLoader 加上这行代码,告警就没了 print(d)

结果为字典类型

{'city': 'shanghai', 'time': 202210, 'successfully': 200, 'exception': 500}

yaml文件与python代码书写比较

yaml基本语法规则:
键值对
大小写敏感
使用缩进表示层级关系
缩进时不允许使用Tab键,只允许使用空格。
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
#表示注释,从这个字符一直到行尾,都会被解析器忽略,这个和python的注释一样
yaml支持的数据结构有三种:
对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
纯量(scalars):单个的、不可再分的值。字符串、布尔值、整数、浮点数、Null、时间、日期

字典嵌套字典 #字典嵌套字典 #python代码书写为{'dd': 'city2:shanghai2 time2:2022102'} #dd: # city2: shanghai2 # time2: 2022102 列表(或叫数组)中嵌套字典,字典中一组数据 #列表(或叫数组)中嵌套字典 #python代码书写为[{'city2': 'shanghai2'}, {'time2': 2022102}, {'successfully': 200}] #yaml里面写一个列表,前面加一个'-'符号 - city2: shanghai2 - time2: 2022102 - successfully: 200 列表(或叫数组)中嵌套字典,字典中多组数据 #列表(或叫数组)中嵌套字典 #python代码书写为[{'city3': 'shanghai3', 'time3': 2022103}, {'city4': 'shanghai4', 'time4': 2022104}, {'city5': 'shanghai5', 'time5': 2022102}] - city3: shanghai3 time3: 2022103 - city4: shanghai4 time4: 2022104 - city5: shanghai5 time5: 2022102 字典嵌套列表 #字典嵌套列表 #python代码书写为{'color1': ['red', 1], 'color2': ['bule', 2], 'color3': ['black', 3]} color1: - red - 1 color2: - bule - 2 color3: - black - 3 组合使用 #组合使用 #python代码书写为{'languages': ['Ruby', 'Perl', 'Python', 'java c++ shell'], 'websites': {'YAML': 'yaml.org', 'Ruby': 'ruby-lang.org', 'Python': 'python.org', 'Perl': 'use.perl.org'}, 'db': {'host': 'xxx', 'port': 3306, 'user': 'shanghai', 'password': 'xxx', 'db_name': 'china', 'db_type': 'mysql'}} languages: - Ruby - Perl - Python - java c++ shell websites: YAML: yaml.org Ruby: ruby-lang.org Python: python.org Perl: use.perl.org db: host: xxx port: 3306 user: shanghai password: xxx db_name: china db_type: mysql 其他类型 #1、数值直接以字面量的形式表示 #number: 202210.010101 #{'number': 202210.010101} #2、布尔值用true和false表示 #isSet: true #{'isSet': True} #isSet1: false #{'isSet1': False} #3、null用~表示 #parent: ~ #{'parent': None} #4、时间采用 ISO8601 格式。 #time1: 2022-10-10t10:10:10.10-10:00 #{'time1': datetime.datetime(2022, 10, 10, 10, 10, 10, 100000, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=50400)))} ##5、日期采用复合 iso8601 格式的年、月、日表示。 #date: 2022-10-01 #{'date': datetime.date(2022, 10, 1)} #6、YAML 允许使用两个感叹号,强制转换数据类型,转换成字符串str。 #int_to_str: !!str 123 #{'bool_to_str': '123'} #bool_to_str: !!str true #{'bool_to_str': 'true'}

到此这篇关于python使用yaml格式文件的方法的文章就介绍到这了,更多相关python使用yaml内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



yaml 方法 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章