将csv文件解析为向量并转换为适当的数据类型 [英] Parse csv file into vector and convert to appropriate data types

查看:143
本文介绍了将csv文件解析为向量并转换为适当的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在使用c ++解析一个CSV文件,我已经成功解析了数据,但现在我正在尝试将每个列转换为适当的类型,因为我声明了a-j。我只是说一些数字dd.a = stod(vector [0])例如我希望这是有效的,因为数据将被连续发送到csv文件所以可以有n行,但我知道有只有10列。我只是想确保通过做我正在做的事情,每个变量将在每次扫描后得到它的数据?并且不会跳过任何数据行?



Hello all I'm working with Parsing a CSV file with c++, I have successfully parsed the data but now I'm trying to convert each column to its appropriate type as I have declared a-j. I have it just saying somenumbers dd.a = stod(vector[0]) for example I'm hoping this is valid cause the data will be sent to a csv file continuously so there can be n number of rows but I know there is only 10 columns. I just want to make sure that by doing what I'm doing that each variable will gets its data after each scan? and no row of data will be skipped?

	typedef struct {
		int a=0;
		int b=0;
		int c=0;
		double d=0;
		double e=0;
		double f=0;
		double g=0;
		int h=0;
		double i=0;
		double j=0;
	}somenumbers;

	vector<vector<string> > matrix;
	somenumbers dd;
	string line;
	ifstream file("mynumbers - Copy.csv");
	
	if (!file.is_open())
	perror("error while opening file");
	
	
	getline(file, line, '\n');

	while (getline(file, line, '\n')) //find the endline charcater
	{

		vector<string> vec;
		string linevec;
		istringstream ss(line);//allows for parsing each line 

		while (getline(ss, linevec, ','))
		{

			vec.push_back(linevec);//push the parsed data for that line into a vector

		}

		
		dd.a= stod(vec[0]);
		dd.b = stod(vec[1]);
		cout << dd.a << endl;
		
		matrix.emplace_back(vec);//push each parsed line into another vector
		
		
}





我尝试了什么:



我已成功获得所有结束字符和逗号

csv文件的格式如下

a,b,c,d,e,f,g,h,i,j

1.,









n。,行



What I have tried:

I have successfully got all the end character and commas
the csv file is formatted as follow
a,b,c,d,e,f,g,h,i,j
1.,
.
.
.
.
n., rows

推荐答案

它很容易测试你的循环



得到一条线

将一行划分为逗号分隔的'变量'



- 我首先集中精力...这虽然



Its easy enough to test your loop(s) to

get a line
split a line into comma separated 'variables'

- I'd concentrate on that first ... this though

getline(file, line, '\n');

  while (getline(file, line, '\n')) //find the endline charcater





ok,所以,你会得到一条线,立即丢弃它并获得另一条线(在此期间) - 是否有理由丢弃第一条线?我理解它是否是一个'标题'行,但是,你可能希望在代码中评论这个事实,以便其他人阅读得到'为什么'



这段代码 - 我假设只是测试





ok, so, you're going to get a line, immediately discard it and get another line (in the while) - is there a reason for discarding the first line ? I understand if its a 'header' line, but then, you may wish to comment that fact in the code, so that anyone else reading gets the 'why'

this code - Im presuming is just testing

dd.a= stod(vec[0]);
       dd.b = stod(vec[1]);





你真的不会硬编码到vec [你愿意吗?你会使用FOR循环,如果你100%确定只有10列,或者在'vec'上使用迭代器 - 个人,我会使用迭代器,保持计数,如果你最终超过10列停止运行



好的,我回头看了看这个





you wouldnt really hard code all the way to vec[9] would you ? you would use a FOR loop for if you were 100% sure there was only ever going to be 10 columns, or, use an iterator on 'vec' - personally, I would use the iterator, keep a count, and if you end up with more than 10 columns stop the run

[edit] ok, I looked back and saw this

typedef struct {
      int a=0;
      int b=0;
      int c=0;
      double d=0;
       double e=0;
       double f=0;
       double g=0;
       int h=0;
      double i=0;
       double j=0;
   }somenumbers;





你打算将'csv'文件的每一行解析为'somenumbers'结构?如果是这样,我说的一切仍然适用 - 在这种情况下我可能仍然使用迭代器/计数器从vec []分配给某些数字,如





is it your intent to parse each line of the 'csv' file into a 'somenumbers' struct ? if so, everything I said still applies - in that case I'd probably still use an iterator/counter to assign from the vec[] to somenumbers, as in

...
switch(fieldNumber) :
case 0 : a = convertToInt(vec[fieldNumber]);   
case 1 : b = convertToInt(vec[fieldNumber]);   
...





其中convertToInt是你必须从字符串转换为Int的函数,你也可以需要一个convertToDouble我猜



[/ edit]



where convertToInt is some function you have to convert from a string to an Int, you'd also need a convertToDouble I guess

[/edit]


是的第一行getline的原因是因为标题行。



是的我正在测试希望有更好的方法我不确定如何在vec上使用迭代器你能解释一下吗?

并且我也将vec坚持到矩阵中,所以我会在矩阵上使用迭代器吗?

最后是的,struct somenumbers与列类型有关,所以当我看到第1列时,我知道int ...等等。
Yes the reason for the getline on the first Line is cause that the header line.

Yes I was testing an hoping for a better way I'm not to sure how to use the iterator on the vec can you explain?
and also I'm sticking vec into matrix as well so would I use iterator on matrix instead?
Last yes the "struct somenumbers" relates to the column types so that why that's there so when I see column 1 I know int ... and so on.


这篇关于将csv文件解析为向量并转换为适当的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆