如何从csv文件中读取整数值 [英] How to read integer values from csv file

查看:166
本文介绍了如何从csv文件中读取整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我读了一个csv文件,其中包含不同类型的数据,如字符串,整数等。我想从该文件中仅获取整数数据并存储在vector.Below i copy一些代码从文件中读取行,存储在vector.I尝试将字符串转换为整数或双精度并对其进行一些操作,因为我知道字符串值,但如何只识别整数值并将其存储在向量中?



Hello,
I read one csv file in which it contains different types of data like string, integer etc.I want to fetch only integer data from that file and store in vector.Below i copy some code that read line from file,store in vector.I tried to convert string to integer or double and do some operations on it as i know that string values,but how to identify only integer values and store that in vector?

#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<sstream>
#include<iterator>
#include<algorithm>
using namespace std;

int main() 
{
	string line;
	int cnt=0,val1,val2;
	
	vector<string> vf;
	
	ifstream in("TestValues.csv");
	ofstream out("result.csv");

	while(getline(in,line,','))
	{
		vf.push_back(line);
	}	
	
	for(int i=0;i<vf.size();i++)
	{	
		val1=atoi(vf[i].c_str());
		out<<val1;
	}	

	in.close();
	out.close();
	system("pause");

	return 0;
}



i / p文件数据如


i/p file data like

Employee Code:      121A    Employee Name :   David
    Date      InTime  OutTime Shift  Total Duration    Status
    01-Apr-02  9:59    19:53   FS        9:53         Present





我想只处理员工代码来排序记录该月的员工的工作时间,工作时间和总工作时间。如何做?



I want to deal with only Employee code to sort record of Intime,OutTime and Total Duration of working of employee in that month.How to do that?

推荐答案

Venkat



当您访问csv文件时,您的第一个值是员工代码,因此我们可以假设一个关系为

第一个值 - 员工代码

2价值 - 员工姓名

3价值 - 日期



等等



8价值 - 状态



因此,第一个位置之后的第9个位置是您的员工代码。同样地,你可以设计第4个位置后每9个位置就是你的InTime。



这样你就可以访问每个值并保持向量并随心所欲地执行它们。



希望这有帮助
Venkat

When you access the csv file your first value is Employee Code, thus we can assume a relationship as
First Value -- Employee Code
2 Value -- Employee Name
3 Value -- Date

and so on to

8 Value -- Status

So every 9th position after the first is your Employee Code. Similarly you can devise that every 9 the position after 4th is your InTime.

This way you can access each value and maintain vectors and do what ever you wish with them.

Hope this helps


我的第一个问题是数据文件是如何继续的。是继续为另一名员工继续另外三行录入,还是继续为同一名员工继续录制日期(即每个档案只有一名员工)?



无论哪种方式,你都必须解析你读入的字符串。我写了几个实用程序类来分割你可能会觉得有用的字符串:

拆分字符串

再次拆分字符串 - strtok兑换



假设每个文件只有一个empolyee,许多日期,并且格式一致,我做这样的事情:



My first question is how the data file continues. Does it continue with another three-line entry for another employee, or does it continue with further dates for the same employee (i.e. there is only one employee per file)?

Either way, you'll have to parse the string you read in. I wrote a couple of utility classes for splitting strings that you might find useful:
Splitting strings
Splitting strings again – strtok redeemed

Assuming that each file only has one empolyee, many dates, and that the format is consistent, I'd do something like this:

// Open file
...

// Read employee line
string line;
string empCode;
if (!getline(in,line))
{
    // Error
    return -1;
}
else
{
    // Get employee code using my string tokeniser
    // from "Splitting strings again – strtok redeemed"
    string_tokeniser tokeniser(line);
    // First token "Employee Code:" ends with colon
    tokeniser.next(':');
    // Employee code ends here
    tokeniser.next("Employee Name :");
    tokeniser.get_token(empCode);
    // empCode now contains "      121A    "
    // trim spaces to taste
}
// Next line is header, so read and throw away
if (!getline(in,line))
{
    // Error
    return -1;
}
// Now we come to the meat.
while (getline(in,line))
{
    // Split the line up into substrings
    vector<string> lineElements;
    // Using the string splitter from "Splitting strings"
    // to split on space and ignore empty substrings
    tokenise_string(line, ' ', lineElements);
    // Should have six fields
    // (date, in, out, shift, total, status)
    if (lineElements.size() < 6)
    {
        return -1;
    }
    // Write to output file
    out << empCode << ',' << lineElements[1] << ',' <<
         lineElements[2] << ',' <<  lineElements[4] << endl;
}


这篇关于如何从csv文件中读取整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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