C# 使用streamreader读取csv时如何跳过第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9093223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to skip first line while reading csv using streamreader
提问by Ye Myat Aung
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.
我有以下代码可以从 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());
}
}
}
Sample input csv looks like this:
示例输入 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
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
回答by Jon Skeet
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.)
(我个人不喜欢使用 Peek。)
Then when you write out the output, start with headerLine.
然后当你写出输出时,从headerLine.
回答by cjk
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.
您可以在 while 循环之前读取第一行并存储它,或者您可以使用计数器/布尔字段来检查您在文件中的位置。
回答by gdoron is supporting Monica
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();
//***//
}
}
回答by Arion
Something like this:
像这样的东西:
bool isFirst=true;
using (StreamReader sr = new StreamReader(filePath))
{
while (sr.Peek() != -1)
{
if(isFirst)
{
isFirst=false;
continue;
}
}
}
回答by Muhammad Faiq
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..
}

