如何使用StreamReader读取CSV文件时跳过第一行?

31

我有以下代码从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
6个回答

79

在进入循环之前先仔细阅读它。我会这样做:

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

(个人而言,我不喜欢使用Peek。)

然后在编写输出时,以headerLine开头。


我很少使用Peek,但只是好奇...你为什么不喜欢使用Peek? - Justin
1
@Justin:我倾向于担心下一次读取数据时可能会发生更改-“读一次并执行”比“读取,检查,然后再读取”感觉更清洁,并假设两次读取将具有相同的结果。当然,使用的任何抽象(在这种情况下为StreamReader)的缓冲细节可以保证Peek的结果与下一次读取一致,但是不必担心这一点会更加清洁。 - Jon Skeet

20

只需阅读第一行,不做任何其他操作...

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

        //***//
    }
}

6

类似这样:

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

2
我建议使用技术来处理这种情况,
                int row = 0;
                using (StreamReader sr = new StreamReader(filePath))

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

1

在 while 循环之前,您可以读取第一行并将其存储起来,或者您可以使用计数器 / 布尔字段来检查文件中的位置。


-1
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++;
                    }
                }
            }

1
你的回答可以通过提供更多的支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接