Python3解析yaml文件

  • 解析代码
# -*- coding: utf-8 -*-
"""比对引擎:创建库功能接口相关操作"""

"""导入yaml 模块"""
import yaml
import os

class read_config:
    def __init__(self):
        self.host = None
        self.lib_batch_list = None
        self.lib_person_filter = None
        self.lib_batch_feature_add = None
        self.lib_batch_feature_remove = None
        self.lib_image_search = None
        self.lib_feature_search = None
        self.feature_nvn = None
        self.lib_fuse_image_add = None
        self.lib_fuse_feature = None

        self.color_param = None
        self.depth_param = None
        self.matrix_rotation = None
        self.matrix_translation = None
        return

    def read(self, cfg_file):
        try:
            print('Current directory is ', os.getcwd())
            #f = open(os.path.join("../common/", "config.yaml"), encoding="UTF-8")
            f = open(os.path.join(cfg_file), encoding="UTF-8")
            d = yaml.safe_load(f)
            print("类型:", type(d))
            print(d)
            self.host = d['address']['host']
            self.lib_batch_list = self.host + d['address']['lib_batch_list']
            self.lib_person_filter = self.host + d['address']['lib_person_filter']
            self.lib_batch_feature_add = self.host + d['address']['lib_batch_feature_add']
            self.lib_batch_feature_remove = self.host + d['address']['lib_batch_feature_remove']
            self.lib_image_search = self.host + d['address']['lib_image_search']
            self.lib_feature_search = self.host + d['address']['lib_feature_search']
            self.feature_nvn = self.host + d['address']['feature_nvn']
            self.lib_fuse_image_add = self.host + d['address']['lib_fuse_image_add']
            self.lib_fuse_feature = self.host + d['address']['lib_fuse_feature']

            self.color_param = d['parameters']['color_param']
            self.depth_param = d['parameters']['depth_param']
            self.matrix_rotation = d['parameters']['matrix_rotation']
            self.matrix_translation = d['parameters']['matrix_translation']
            print(type(self.color_param))
            return self
        except Exception as e:
            print('Exception error: ', e.__str__())
            return None

# 这个main只是用于运行演示,作为Robot Framework的库是不需要的
if __name__ == '__main__':
    obj = read_config()
    ret = obj.read()
    if ret is not None:
        print('Call feature_succ succ')
    else:
        print('Call feature_succ fail')

    print('ret: ', ret)

    print(obj.lib_image_search)
  • 附config.yaml文件
address:
    host: "http://10.0.1.237:8081"
    lib_batch_list: "/engine/lib/batch/list"
    lib_person_filter: "/engine/lib/person/filter"
    lib_batch_feature_add: "/engine/lib/batch/feature/add"
    lib_batch_feature_remove: "/engine/lib/batch/feature/remove"
    lib_image_search: "/engine/lib/image/search"
    lib_feature_search: "/engine/lib/feature/search"
    feature_nvn: "/engine/feature/nvn"
    lib_fuse_image_add: "/engine/lib/fuse/image/add"
    lib_fuse_feature: "/engine/lib/fuse/feature"

parameters:
    param_group_id: "101"
    id_4: "0000000000000004"
    color_param:
        - 1548.2
        - 1547.44
        - 528.05
        - 975.372
    depth_param:
        - 473.5505
        - 473.454
        - 195.776
        - 317.486
    matrix_rotation:
        - 0.999478
        - -6.72118
        - 0.0322943
        - 0.00143971
        - 0.999096
        - -0.0424784
        - -0.0322623
        - 0.0425028
        - 0.998575
    matrix_translation:
        - 11.6871
        - -0.309198
        - 1.01959
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值