如何使用streamreader读取csv时跳过第一行

发布于 2024-12-31 22:10:48 字数 2036 浏览 0 评论 0原文

我有以下代码来从 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

来世叙缘 2025-01-07 22:10:49

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

You could read the first line before your while loop and store it, or you could use a counter / boolean field to check where in the file you are.

云醉月微眠 2025-01-07 22:10:49
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++;
                    }
                }
            }
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++;
                    }
                }
            }
无法言说的痛 2025-01-07 22:10:48

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

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

我个人不喜欢使用 Peek。)

然后,当您写出输出时,从 headerLine 开始。

从 C# 7 开始,模式匹配让这变得更加愉快:

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

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.

As of C# 7, pattern matching makes this more pleasant:

using (StreamReader sr = new StreamReader(filePath))
{
    string headerLine = sr.ReadLine();
    while (sr.ReadLine() is string line)
    {
          ...
    }
}
旧伤还要旧人安 2025-01-07 22:10:48

只需阅读第一行,然后不执行任何操作...

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

        //***//
    }
}

Just read the first line and do nothing with it...

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

        //***//
    }
}
木槿暧夏七纪年 2025-01-07 22:10:48

像这样的事情:

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

Something like this:

bool isFirst=true;
using (StreamReader sr = new StreamReader(filePath))
{
    while (sr.Peek() != -1)
    {
        if(isFirst)
        {
            isFirst=false;
            continue;
        }
    }
}
青芜 2025-01-07 22:10:48

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

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

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

I recommend, to use technique to handle such situation,

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

                {
                   While(reader.Read()) //Each row of the file
                    {
                        row++;
                        if(row==1)
                        {
                            continue;
                        }
               }
... code..
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文