本文介绍了如何跳过第一行,而使用的StreamReader读取CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code读取csv文件的价值观和做一些处理。我想跳过输入csv文件的第一行,因为它包含标题文本,但我想把它加回处理完成后。

 列表<串GT;值=新的List<串GT;();
使用(StreamReader的SR =新的StreamReader(文件路径))
{
    而(sr.Peek()!= - 1)
    {
        串线= sr.ReadLine();
        清单<串GT; lineValues​​ = line.Split(,)了ToList();
        变种tempMinInt = 1;
        变种tempValue = 1;
        变种tempValInt = Convert.ToInt32(lineValues​​ [4]);
        如果(lineValues​​ [3] ==1876年)
        {
            如果(tempValInt%60!= 0)
            {
                tempMinInt =(tempValInt / 60)+ 1;
                tempValue = tempMinInt * 30;
            }
            其他
            {
                tempMinInt = tempValInt / 60;
                tempValue = tempMinInt * 30;
            }
        }
        否则如果(lineValues​​ [3] ==1875年)
        {
            如果(tempValInt!= 0)
            {
                tempValue = 500;
            }
            其他
                tempValue = 0;
        }        如果(lineValues​​ [3] ==1876年)
        {
            values​​.Add(的string.join(,,lineValues​​)+,+0+,+30+,+ tempValue.ToString());
        }
        否则如果(lineValues​​ [3] ==1875年)
        {
            values​​.Add(的string.join(,,lineValues​​)+,+1+,+500+,+ tempValue.ToString());
        }    }
}

样品输入CSV看起来是这样的:

  ID,日期时间,MSISDN,NUM,持续时间
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

和我想有我的输出是这样的:

  ID,日期时间,MSISDN,NUM,持续时间,类型,ammount的,总
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


解决方案

只是第一次读它,你进入循环之前。我会做到这一点:

 使用(StreamReader的SR =新的StreamReader(文件路径))
{
    字符串headerLine = sr.ReadLine();
    串线;
    而((行= sr.ReadLine())!= NULL)
    {
         ...
    }
}

(我不喜欢使用窥视,个人)。

然后,当你写出来的输出,开始与 headerLine

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
解决方案

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.

这篇关于如何跳过第一行,而使用的StreamReader读取CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:26