pip install pyyaml
因为在5.1之后,直接用load()已经过时了,需要在方法里面加一个 loader 的请求参数,如下:
def read_yaml(self, path):
withopen(path, encoding="utf-8") as f:
result = f.read()
result = yaml.load(result, Loader=yaml.FullLoader) #loader可选择BaseLoader、SafeLoader、FullLoader、UnsafeLoaderreturnresult
也可以,根据Python语法转换,如下:
def read_yaml(self, path):
withopen(path, encoding="utf-8") as f:
result = f.read()
result = yaml.full_load(result)
return result
BaseLoader: 仅加载最基本的YAML
SafeLoader: 安全地加载YAML语言的子集。建议用于加载不受信任的输入(safe_load)
FullLoader: 加载完整的YAML语言。避免任意代码执行。这是当前(PyYAML 5.1)默认 加载器调用 yaml.load(input) (发出警告后)(full_load)
UnsafeLoader(也称为Loader向后兼容性):原始的Loader代码,可以通过不受信任的数据输入轻松利用。(unsafe_load)
注意⚠️,在5.1之后如果要使用yaml.load的时候就需要 + Loader参数来使用加载器
yaml.load(result, Loader=yaml.FullLoader) # 加载完整的YAML语言
如果字符串或者文件中包含多个YAML文档,那么可以使用 yaml.load_all 函数将它们全部反序列化,得到的是一个包含所有反序列化后的YAML文档的生成器对象:
yaml.load_all(document, Loader=yaml.FullLoader)
with open(path, "w", encoding="utf-8") as f:
yaml.dump(data, f, Dumper=yaml.SafeDumper) # 可选BaseDumper、SafeDumper
# YAML中使用‘#’ 来表示注释(‘#’前面要有一个空格)
使用破折号(dash) - 后跟一个空格(Space)来表示序列中的项。
- id
- name
- age
with open('yaml文件地址', encoding="utf-8") as f:
result = f.read()
result = yaml.load(result, Loader=yaml.FullLoader)
print(result)
['id', 'name', 'age']
在嵌套的块序列中,内层的序列可以直接从当前行开始而不必从新的一行开始
- - Python
- Ruby
- JavaScript
- PHP
- - Unix
- Linux
- Windows
with open('yaml文件地址', encoding="utf-8") as f:
result = f.read()
result = yaml.load(result, Loader=yaml.FullLoader)
print(result)
[['Python', 'Ruby', 'JavaScript', 'PHP'], ['Unix', 'Linux', 'Windows']]
块内容中,使用冒号 : 后跟一个空格来分隔映射中的键和值。
name: bob
age: 28
gender: Male
{'name': 'bob', 'age': 28, 'gender': 'Male'}
复杂的键使用问号 ? 后跟一个空格来表示,如下所示
? !!python/tuple [0, 0]
: Start
? !!python/tuple [3, 5]
: End
{(0, 0): 'Start', (3, 5): 'End'} # 列表不能成为键
Employee:
Job_title: Employee
Salary: 5000
Annual Leave: 10
Manager:
Job_title: Manager
Salary: 8000
Annual Leave: 15
{'Employee': {'Job_title': 'Employee', 'Salary': 5000, 'Annual Leave': 10},
'Manager': {' Job_title': 'Manager', 'Salary': 8000, 'Annual Leave': 15}}
- name: PyYAML
status: 4
license: MIT
language: Python
- name: PySyck
status: 5
license: BSD
language: Python
[{'name': 'PyYAML', 'status': 4, 'license': 'MIT', 'language': 'Python'},
{'name': 'PySyck', 'status': 5, 'license': 'BSD', 'language': 'Python'}]
Programing Languages:
- Java
- Swift
- C++
- Go
Operation System:
- Unix
- Linux
- Windows
- OSX
{'Programing Languages': ['Java', 'Swift', 'C++', 'Go'],
'Operation System': ['Unix', 'Linux', 'Windows']}
class YamlUtil:
__instance = None
def __new__(cls, *args, **kwargs):
if not cls.__instance:
print("YamlUtil first init")
cls.__instance = super(YamlUtil, cls).__new__(cls, *args, **kwargs)
return cls.__instance
def read_yaml(self, path):
with open(path, encoding="utf-8") as f:
result = f.read()
result = yaml.load(result, Loader=yaml.FullLoader)
return result
def write_yaml(self, path, data):
with open(path, "w", encoding="utf-8") as f:
yaml.dump(data, f, Dumper=yaml.SafeDumper)
yamlUtil = YamlUtil()