1.txt文档写入二维列表:python fgt = open(gtpath, 'r')###得到fgt的每个txt文档
for line in fgt.readlines(): line = list(line.strip().split(' ')) s = [] for i in line: s.append(int(i)) resultgt.append(s) print(len(resultgt[0]))
2.txt文档每一行写入vector
void txt_to_vectordouble(std::vector<std::vector<double>>& res, std::string pathname)
{
ifstream infile;
infile.open(pathname.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
vector<double> Point;//txt文档每一行保存到Point中
string s;
while (getline(infile, s)) {
istringstream is(s); //将读出的一行转成数据流进行操作
double d;
while (!is.eof()) {
is >> d;
Point.push_back(d);
}
res.push_back(Point);
Point.clear();
s.clear();
}
infile.close(); //关闭文件输入流
}
vector<vector<double>> data;
txt_to_vectordouble(data, imgdir + "/labels/000002.txt");