第 0 行第 0 列错误:加载 YAML 文件时转换错误
Error at line 0, column 0: bad conversion while loading YAML file
下面是我已经通过 YAMLint 验证过的 yaml 示例文件
URL 但在使用 yaml-cpp 库函数 LoadFile
加载此文件时,出现错误:
"Error at line 0, column 0: bad conversion while loading YAML file"
示例 yaml 文件:
%YAML 1.1
---
name: abcd_server_interface
spec_type: interface
spec_version: 1
description: 'Interface for capability ABCD Slam'
default_provider: abcd_server/abcd_server_provider
interface: {}
topics: {
provides: {
provide: {
name: '/3d_pose_graph',
type: '-',
description: '-',
},
provide: {
name: '/key_frame_msgs',
type: '-',
description: '-'
},
provide: {
name: '/pc_map',
type: 'sensor_msgs/PointCloud2',
description: 'Point cloud'
},
provide: {
name: '/3dOctomap',
type: '-',
description: '-'
},
provide: {
name: '/poseOfRobots',
type: '-',
description: '-'
},
provide: {
name: '/occupied_calls_vis_array',
type: '-',
description: '-'
},
},
requires: {
require: {
name: '/image_depth_throttled',
type: 'sensor_msgs/Image',
description: 'Sensor Image',
},
require: {
name: '/image_color_throttled/compressed',
type: 'sensor_msgs/CompressedImage',
description: 'Sensor compressed image',
},
require: {
name: '/statistics',
type: '-',
description: '-',
},
require: {
name: '/initialpose',
type: '-',
description: '-',
},
require: {
name: '/reset_rgbdserver',
type: 'std_msgs/Bool',
description: 'To reset server',
},
require: {
name: '/scan',
type: 'sensor_msgs/LaserScan',
description: 'laser scan data',
},
require: {
name: '/tf',
type: 'tf2_msgs/TFMessage',
description: 'tf',
},
require: {
name: '/tf_static',
type: 'tf2_msgs/TFMessage',
description: 'tf static',
},
}
}
Parameter: {
parameter: {
name: 'max_delay',
value: '100'
},
parameter: {
name: 'rgb_hints',
value: 'compressed'
},
parameter: {
name: 'depth_hints',
value: 'raw'
},
parameter: {
name: 'bot_frame',
value: '/base_link'
},
parameter: {
name: 'kinect_frame',
value: '/camera_depth_optical_frame'
},
parameter: {
name: 'minZ',
value: '-1.0'
},
parameter: {
name: 'maxZ',
value: '4.0'
},
}
这是我编写的用于替换 yaml 文件中参数部分的参数名称 (max_delay
) 值的代码。
#include <fstream>
#include <iostream>
#include "yaml-cpp/yaml.h"
using namespace std;
bool UpdateYAMLFile(const string& sYamlFile, const string& sParamName2Update, const string sValue)
{
bool bRet = false;
try
{
//YAML::Node head_ = YAML::LoadFile(sYamlFile);
YAML::Node head_ = YAML::LoadFile(sYamlFile);
//YAML::Node head_ = YAML::Load(sYamlFile);
std::cout << head_.size();
cout<<"ramesh"<<endl;
for (YAML::iterator ith = head_.begin();ith != head_.end(); ++ith)
{
YAML::Node sub1_ = ith->second;
for (YAML::iterator itc = sub1_.begin();itc != sub1_.end(); ++itc)
{
string sParam = itc->second["name"].as<std::string>(); //cout << sParam << endl;
if(sParam == sParamName2Update)
{
itc->second["value"] = sValue;
bRet = true;
}
}
}
ofstream fout(sYamlFile);
fout << head_; //cout << head_ << "\n";
}
catch (const YAML::Exception& e)
{
cout << "ERROR: Updation of yaml file " << sYamlFile << "Failed: Exception: " << e.what() << "\n";
}
return bRet;
}
//driver
int main(int argc, char** argv)
{
int bRet = UpdateYAMLFile("sample.yaml", "max_delay","1150");
if(bRet == true)
cout << "SUCCESS: YAML file updated..\n";
else
cout << "ERROR: YAML file update FAILED...\n";
return 0;
}
基本上UpdateYAMLFile
API 将更新示例yaml 文件中的max_dealy
参数值。使用这个 API 我应该能够更新参数部分中的所有参数(max_delay
、rgb_hints
、depth_hints
、minZ
、maxZ
)示例 yaml 文件。
命令:
g++ -std=c++0x yaml.cpp -o res /usr/lib/x86_64-linux-gnu/libyaml-cpp.a
您的地图访问似乎差了一个。您的地图嵌套了三层:
topics: { // first key/value pair
provides: { // second key/value pair
provide: { // third key/value pair
name: '/3d_pose_graph', // now lookup the name attribute
type: '-',
description: '-',
}
}
}
您的第三张地图也有问题:所有的键都是 "provide"
。这不是真正的地图,它将被解析为单个 key/value 对(每个都覆盖前一个)。您可以将其制成如下列表:
topics: {
provides: [ // this is now a list
{
name: '/3d_pose_graph',
type: '-',
description: '-',
}, {
name: '/key_frame_msgs',
type: '-',
description: '-'
}, ...
]
}
此外,您不必在不知道其中内容的情况下盲目地遍历地图的值。为什么不直接访问密钥:
for (YAML::Node provides : head_["topics"]["provides"]) {
if (provides["name"].as<std::string>() == sParamName2Update) {
provides["name"] = sValue;
}
}
下面是我已经通过 YAMLint 验证过的 yaml 示例文件
URL 但在使用 yaml-cpp 库函数 LoadFile
加载此文件时,出现错误:
"Error at line 0, column 0: bad conversion while loading YAML file"
示例 yaml 文件:
%YAML 1.1
---
name: abcd_server_interface
spec_type: interface
spec_version: 1
description: 'Interface for capability ABCD Slam'
default_provider: abcd_server/abcd_server_provider
interface: {}
topics: {
provides: {
provide: {
name: '/3d_pose_graph',
type: '-',
description: '-',
},
provide: {
name: '/key_frame_msgs',
type: '-',
description: '-'
},
provide: {
name: '/pc_map',
type: 'sensor_msgs/PointCloud2',
description: 'Point cloud'
},
provide: {
name: '/3dOctomap',
type: '-',
description: '-'
},
provide: {
name: '/poseOfRobots',
type: '-',
description: '-'
},
provide: {
name: '/occupied_calls_vis_array',
type: '-',
description: '-'
},
},
requires: {
require: {
name: '/image_depth_throttled',
type: 'sensor_msgs/Image',
description: 'Sensor Image',
},
require: {
name: '/image_color_throttled/compressed',
type: 'sensor_msgs/CompressedImage',
description: 'Sensor compressed image',
},
require: {
name: '/statistics',
type: '-',
description: '-',
},
require: {
name: '/initialpose',
type: '-',
description: '-',
},
require: {
name: '/reset_rgbdserver',
type: 'std_msgs/Bool',
description: 'To reset server',
},
require: {
name: '/scan',
type: 'sensor_msgs/LaserScan',
description: 'laser scan data',
},
require: {
name: '/tf',
type: 'tf2_msgs/TFMessage',
description: 'tf',
},
require: {
name: '/tf_static',
type: 'tf2_msgs/TFMessage',
description: 'tf static',
},
}
}
Parameter: {
parameter: {
name: 'max_delay',
value: '100'
},
parameter: {
name: 'rgb_hints',
value: 'compressed'
},
parameter: {
name: 'depth_hints',
value: 'raw'
},
parameter: {
name: 'bot_frame',
value: '/base_link'
},
parameter: {
name: 'kinect_frame',
value: '/camera_depth_optical_frame'
},
parameter: {
name: 'minZ',
value: '-1.0'
},
parameter: {
name: 'maxZ',
value: '4.0'
},
}
这是我编写的用于替换 yaml 文件中参数部分的参数名称 (max_delay
) 值的代码。
#include <fstream>
#include <iostream>
#include "yaml-cpp/yaml.h"
using namespace std;
bool UpdateYAMLFile(const string& sYamlFile, const string& sParamName2Update, const string sValue)
{
bool bRet = false;
try
{
//YAML::Node head_ = YAML::LoadFile(sYamlFile);
YAML::Node head_ = YAML::LoadFile(sYamlFile);
//YAML::Node head_ = YAML::Load(sYamlFile);
std::cout << head_.size();
cout<<"ramesh"<<endl;
for (YAML::iterator ith = head_.begin();ith != head_.end(); ++ith)
{
YAML::Node sub1_ = ith->second;
for (YAML::iterator itc = sub1_.begin();itc != sub1_.end(); ++itc)
{
string sParam = itc->second["name"].as<std::string>(); //cout << sParam << endl;
if(sParam == sParamName2Update)
{
itc->second["value"] = sValue;
bRet = true;
}
}
}
ofstream fout(sYamlFile);
fout << head_; //cout << head_ << "\n";
}
catch (const YAML::Exception& e)
{
cout << "ERROR: Updation of yaml file " << sYamlFile << "Failed: Exception: " << e.what() << "\n";
}
return bRet;
}
//driver
int main(int argc, char** argv)
{
int bRet = UpdateYAMLFile("sample.yaml", "max_delay","1150");
if(bRet == true)
cout << "SUCCESS: YAML file updated..\n";
else
cout << "ERROR: YAML file update FAILED...\n";
return 0;
}
基本上UpdateYAMLFile
API 将更新示例yaml 文件中的max_dealy
参数值。使用这个 API 我应该能够更新参数部分中的所有参数(max_delay
、rgb_hints
、depth_hints
、minZ
、maxZ
)示例 yaml 文件。
命令:
g++ -std=c++0x yaml.cpp -o res /usr/lib/x86_64-linux-gnu/libyaml-cpp.a
您的地图访问似乎差了一个。您的地图嵌套了三层:
topics: { // first key/value pair
provides: { // second key/value pair
provide: { // third key/value pair
name: '/3d_pose_graph', // now lookup the name attribute
type: '-',
description: '-',
}
}
}
您的第三张地图也有问题:所有的键都是 "provide"
。这不是真正的地图,它将被解析为单个 key/value 对(每个都覆盖前一个)。您可以将其制成如下列表:
topics: {
provides: [ // this is now a list
{
name: '/3d_pose_graph',
type: '-',
description: '-',
}, {
name: '/key_frame_msgs',
type: '-',
description: '-'
}, ...
]
}
此外,您不必在不知道其中内容的情况下盲目地遍历地图的值。为什么不直接访问密钥:
for (YAML::Node provides : head_["topics"]["provides"]) {
if (provides["name"].as<std::string>() == sParamName2Update) {
provides["name"] = sValue;
}
}