Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

c# - How to skip first line while reading csv using streamreader

I have my following code to read values from CSV file and do some processing. I would like to skip the first row of the input CSV file as it contains header text but I'd want to add it back after the processing is done.

List<string> values = new List<string>();
using (StreamReader sr = new StreamReader(filePath))
{
    while (sr.Peek() != -1)
    {
        string line = sr.ReadLine();
        List<string> lineValues = line.Split(',').ToList();
        var tempMinInt = 1;
        var tempValue = 1;
        var tempValInt = Convert.ToInt32(lineValues[4]);
        if (lineValues[3] == "1876")
        {
            if (tempValInt % 60 != 0)
            {
                tempMinInt = (tempValInt / 60) + 1;
                tempValue = tempMinInt * 30;
            }
            else
            {
                tempMinInt = tempValInt / 60;
                tempValue = tempMinInt * 30;
            }
        }
        else if (lineValues[3] == "1875")
        {
            if (tempValInt != 0)
            {
                tempValue = 500;
            }
            else
                tempValue = 0;
        }

        if (lineValues[3] == "1876")
        {
            values.Add(string.Join(",", lineValues) + "," + "0" + "," + "30" + "," + tempValue.ToString());
        }
        else if (lineValues[3] == "1875")
        {
            values.Add(string.Join(",", lineValues) + "," + "1" + "," + "500" + "," + tempValue.ToString());
        }

    }
}

Sample input csv looks like this:

id, datetime, msisdn, num, duration
33083,2011-12-19 05:17:57+06:30,98590149,1875,258
33084,2011-12-19 05:22:28+06:30,98590149,1875,69
33085,2011-12-19 05:23:45+06:30,98590149,1875,151
33086,2011-12-19 05:30:21+06:30,98590149,1875,58
33087,2011-12-19 06:44:19+06:30,949826259,1875,66

And I'd like to have my output like this:

id, datetime, msisdn, num, duration, type, ammount, total
33083,2011-12-19 05:17:57+06:30,98590149,1875,258,1,500,500
33084,2011-12-19 05:22:28+06:30,98590149,1875,69,1,500,500
33085,2011-12-19 05:23:45+06:30,98590149,1875,151,1,500,500
33086,2011-12-19 05:30:21+06:30,98590149,1875,58,1,500,500
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just read it first before you get into the loop. I'd do this:

using (StreamReader sr = new StreamReader(filePath))
{
    string headerLine = sr.ReadLine();
    string line;
    while ((line = sr.ReadLine()) != null)
    {
         ...
    }
}

(I don't like using Peek, personally.)

Then when you write out the output, start with headerLine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...