asp.net 如何使用streamreader在阅读csv时跳过第一行

pu82cl6c  于 2023-11-20  发布在  .NET
关注(0)|答案(6)|浏览(144)

我有我的以下代码从CSV文件中读取值并做一些处理。我想跳过输入CSV文件的第一行,因为它包含标题文本,但我想在处理完成后将其添加回来。

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());
        }

    }
}

字符串
示例输入csv如下所示:

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


我希望我的输出像这样:

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

b91juud3

b91juud31#

在进入循环之前先读一下。我会这样做:

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

字符串
(我个人不喜欢使用Peek。)
然后,当您写出输出时,从headerLine开始。

kyvafyod

kyvafyod2#

只读第一行,什么也不做...

List<string> values = new List<string>();
using (StreamReader sr = new StreamReader(filePath))
{
    sr.ReadLine();
    while (sr.Peek() != -1)
    {
        string line = sr.ReadLine();
        List<string> lineValues = line.Split(',').ToList();

        //***//
    }
}

字符串

p1iqtdky

p1iqtdky3#

就像这样:

bool isFirst=true;
using (StreamReader sr = new StreamReader(filePath))
{
    while (sr.Peek() != -1)
    {
        if(isFirst)
        {
            isFirst=false;
            continue;
        }
    }
}

字符串

mtb9vblg

mtb9vblg4#

我建议,用技巧来处理这种情况,

int row = 0;
                using (StreamReader sr = new StreamReader(filePath))

                {
                   While(reader.Read()) //Each row of the file
                    {
                        row++;
                        if(row==1)
                        {
                            continue;
                        }
               }
... code..
}

字符串

pb3skfrl

pb3skfrl5#

你可以在while循环之前读取第一行并存储它,或者你可以使用一个counter / boolean字段来检查你在文件中的位置。

sqougxex

sqougxex6#

When lineNumber is 1, it will skip the execution part and not process anything from first row.   

string doWork = string.Empty;

   var lineNumber = 1;

   using (StreamReader reader = new StreamReader(fileLocation))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();

                    var docLine = line.Split(',');

                    if (!String.IsNullOrEmpty(line.ToString()))
                    {
                        if (lineNumber != 0)
                        {
                            if (lineNumber > 1)
                            {
                                doWork = "Excecuting";
                            }
                        }
                        lineNumber++;
                    }
                }
            }

字符串

相关问题