Python 对 yaml数据进行 读,写,添加数据操作
背景(background)
在做ROS项目的过程中,需要利用python首先从yaml 文件中读取数据作为函数的输入参数,然后利用这些参数进行计算,最后将计算结果添加并保存到这个yaml 文件的结尾(保留原来数据)(In the process of doing ROS projects, it has happed to me that using Python to first read data from the yaml file as input parameters of the function, then add and save the results of the function to the end of the yaml file (keeping the original data in the yaml files) )。
代码(codes)
# -*- coding: utf-8 -*-
# pip install PyYAML first !
import yaml
# read date
with open('config.yaml', 'r') as f:
config_r = yaml.safe_load(f)
print(config_r['port'])
# add date in the end
data_msg = {
"aa": 77,
"bb"