yaml文件内容
device_info_0: bp: 4900 deviceName: JPF4C19123011893 port: 4700 device_info_1: bp: 4902 deviceName: VBJDU18504012277 port: 4702
项目结构
代码
""" @File : Read_YAML.py @Contact : xxx.com @Modify Time @Author @Version ------------ ------- -------- 2021/12/16 6:19 下午 xxx 1.0 @Desciption: """ import yaml import os class Read_YAML: def __init__(self, file_path=None): if file_path is None: self.file_path = os.path.join(os.path.join(os.path.dirname(os.path.dirname(__file__)), "config"), "deviceconfig.yaml") else: self.file_path = file_path self.data = self.read() def read(self): with open(self.file_path, 'r', encoding="utf-8") as f: content = yaml.load(f, Loader=yaml.FullLoader) return content def get_value(self, dataOne, dataTwo): return self.data[dataOne][dataTwo] if __name__ == "__main__": content = Read_YAML().read() print(content) value = Read_YAML().get_value("device_info_0", "deviceName") print(value)
针对不同的需要,加载器有如下几种类型: baseLoader:仅加载最基本的YAML SafeLoader:安全地加载YAML语言的子集。建议用于加载不受信任的输入。(safe_load) FullLoader:加载完整的YAML语言。避免任意代码执行。这是当前(PyYAML 5.1)默认加载器调用 yaml.load(input) (发出警告后)(full_load) UnsafeLoader(也称为Loader向后兼容性):原始的Loader代码,可以通过不受信任的数据输入轻松利用。(unsafe_load)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)