1.YAML 格式
YAML格式的文件拓展名包括:.yml
和 .yaml
,两个都表示YAML文件;
XML格式的文件拓展名为: .xml
基本语法规则
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格。
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
数据结构:
string: 'foo'
integer: 1234
float: 1234.5
boolean: true
list: [1.0, mixed list]
dictionary: {a: b, c: d}
引用
锚点&
和别名*
,可以用来引用。
defaults: &defaults adapter: postgres host: localhost development: database: myapp_development <<: *defaults test: database: myapp_test <<: *defaults
等同于下面的代码。
defaults: adapter: postgres host: localhost development: database: myapp_development adapter: postgres host: localhost test: database: myapp_test adapter: postgres host: localhost
```&用来建立锚点(
defaults),
<<表示合并到当前数据,
*`用来引用锚点。
https://yaml.org/spec/1.2/spec.pdf
2.python读取yaml配置文件
#read_yaml.yaml
first_name: 111
second_name: 222
third_name: 333
basic_name:
test_name: 444
selected_name:
- 666
- 777
sudo pip install pyyaml
sudo pip3 install pyyaml
#read_yaml.py
# -*- coding:utf-8 -*-
import os
import yaml
current_path = os.path.abspath(os.path.dirname(__file__))
print(current_path)
print(current_path + '/../test_dir')
with open(current_path + '/../test_dir/' + 'read_yaml.yaml', 'r') as f:
temp = yaml.load(f.read())
print(temp)
print(temp['basic_name'])
print(temp['basic_name']['test_name'])
print(temp['basic_name']['selected_name'][0])
3.C++读取yaml配置文件
使用yaml-cpp读取和写入yaml文件
安装yaml-cpp:
git clone https://github.com/jbeder/yaml-cpp.git
编译 yaml-cpp:
cd yaml-cpp # 进入克隆的文件夹
mkdir build
cd build
cmake ..
make
sudo make install
使用yaml-cpp:
修改CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
PROJECT(001)
SET(CMAKE_C_COMPILER g++)
add_compile_options(-std=c++11)
link_directories(/home/windistance/yaml-cpp/build)
include_directories(/home/windistance/yaml-cpp/include/yaml-cpp)
ADD_EXECUTABLE(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} yaml-cpp)
添加头文件
#include "yaml-cpp/yaml.h"
示例:
int main(int argc, char** argv)
{
std::string file_name = "/home/test/read_yaml.yaml";
YAML::Node params = YAML::LoadFile(file_name);
YAML::Node topic = params["basic_name"];
cout<<topic<<endl;
return 0;
}
4.opencv读取yaml配置文件
#include "opencv2/opencv.hpp"
#include <time.h>
using namespace cv;
int main(int, char** argv)
{
//FileStorage fs("test.yml", FileStorage::READE);
FileStorage fs("test.yml", FileStorage::WRITE);
fs << "frameCount" << 5;
time_t rawtime; time(&rawtime);
fs << "calibrationDate" << asctime(localtime(&rawtime));
Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
fs << "features" << "[";
for( int i = 0; i < 3; i++ )
{
int x = rand() % 640;
int y = rand() % 480;
uchar lbp = rand() % 256;
fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
for( int j = 0; j < 8; j++ )
fs << ((lbp >> j) & 1);
fs << "]" << "}";
}
fs << "]";
fs.release();
return 0;
}
%YAML:1.0
frameCount: 5
calibrationDate: "Fri Jun 17 14:09:29 2011\n"
cameraMatrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
distCoeffs: !!opencv-matrix
rows: 5
cols: 1
dt: d
data: [ 1.0000000000000001e-01, 1.0000000000000000e-02,
-1.0000000000000000e-03, 0., 0. ]
features:
- { x:167, y:49, lbp:[ 1, 0, 0, 1, 1, 0, 1, 1 ] }
- { x:298, y:130, lbp:[ 0, 0, 0, 1, 0, 0, 1, 1 ] }
- { x:344, y:158, lbp:[ 1, 1, 0, 0, 0, 0, 1, 0 ] }
https://www.docs.opencv.org/3.4/d4/da4/group__core__xml.html
https://blog.csdn.net/YhL_Leo/article/details/47660943
5.roslaunch文件读取yaml配置文件
.launch文件
<rosparam command="load" ns="test" file="$(find test)/config/read_yaml.yaml"/>
nodeHandle_.param("basic_name/test_name", name, 1);