1. CSV格式定义
逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。通常都是纯文本文件。建议使用WORDPAD或是记事本来开启,再则先另存新档后用EXCEL开启,也是方法之一。
2. 使用标准库读写
2.1. 读CSV文件
std::vector<std::pair<double, Eigen::Isometry3d>> LoadTrajectoryFromFile(const std::string &pose_file) { std::ifstream in_file(pose_file); if (!in_file.is_open()) { LOG(FATAL) << "Unable to open file: " << pose_file; } std::vector<std::pair<double, Eigen::Isometry3d>> trajectory; std::string line_str; while (getline(in_file, line_str)) { if (line_str.empty()) { continue; } std::vector<std::string> strs = StringSplit(line_str, ","); if (strs.size() != 8) { LOG(INFO) << "Find invalid pose line, size: " << strs.size() << line_str; continue; } double timestamp = std::stod(strs[0]); Eigen::Vector3d translation = {std::stod(strs[1]), std::stod(strs[2]), std::stod(strs[3])}; Eigen::Quaterniond quater = {std::stod(strs[7]), std::stod(strs[4]), std::stod(strs[5]), std::stod(strs[6])}; quater.normalized(); Eigen::Isometry3d pose = Eigen::Isometry3d::Identity(); pose.translation() = translation; pose.linear() = quater.toRotationMatrix(); trajectory.emplace_back(std::make_pair(timestamp, pose)); } in_file.close(); return trajectory; }2.2. 写CSV文件
void writeCSV(const std::string& filename, const std::vector<std::vector<std::string>>& data) { std::ofstream file(filename); if (!file.is_open()) { std::cout << "Failed to open the file." << std::endl; return; } for (const auto& row : data) { for (size_t i = 0; i < row.size(); ++i) { file << row[i]; if (i < row.size() - 1) { file << ","; } } file << std::endl; } file.close(); }2. 使用csv-parser读写
2.1. 安装
sudo npm install -g csv-parser2.2. 读CSV文件
void readCSV(const std::string& filename, std::vector<std::vector<std::string>>& data) { io::CSVReader<MAX_COLS> csv(filename); std::vector<std::string> row; while (csv.read_row(row)) { data.push_back(row); } }2.3. 写CSV文件
void writeCSV(const std::string& filename, const std::vector<std::vector<std::string>>& data) { std::ofstream file(filename); if (!file.is_open()) { std::cout << "Failed to open the file." << std::endl; return; } for (const auto& row : data) { for (size_t i = 0; i < row.size(); ++i) { file << row[i]; if (i < row.size() - 1) { file << ","; } } file << std::endl; } file.close(); }3. 使用boost库读写
3.1. 读CSV文件
#include <boost/algorithm/string.hpp> #include <boost/tokenizer.hpp> void readCSV(const std::string& filename, std::vector<std::vector<std::string>>& data) { std::ifstream file(filename); if (!file.is_open()) { std::cout << "Failed to open the file." << std::endl; return; } std::string line; while (std::getline(file, line)) { std::vector<std::string> row; boost::tokenizer<boost::escaped_list_separator<char>> tokenizer(line); for (const auto& cell : tokenizer) { row.push_back(cell); } data.push_back(row); } file.close(); }3.2. 写CSV文件
#include <boost/algorithm/string.hpp> #include <boost/tokenizer.hpp> void writeCSV(const std::string& filename, const std::vector<std::vector<std::string>>& data) { std::ofstream file(filename); if (!file.is_open()) { std::cout << "Failed to open the file." << std::endl; return; } for (const auto& row : data) { std::string line; for (size_t i = 0; i < row.size(); ++i) { line += row[i] + ","; } line = line.substr(0, line.size() - 1); file << line << std::endl; } file.close(); }参考文献
csv是什么格式文件-常见问题-PHP中文网