下面是代码:
void ReadFromCsv()
{
using (var reader = new StreamReader(@"d:\test.csv", Encoding.Default))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
listA.Add(values[0]);
}
Console.WriteLine(listA);
}
}
它是从我的csv文件中阅读的,我得到的一行代码的例子是:
50,2,10,201,10,9090339,24-OCT-21 09.38.38.679000 AM,123456789,24/10/2021 09:39:23,22/10/2021 09:39:37,Sm123456789-SM-20211031-VSR-000123.pdf,,,,,26/01/2022 13:08:58,,2,,0
首先,为什么在行尾有很多逗号?其次,如果我想访问值“10”,该怎么办(这是第5个值),这可能吗?或者更进一步,我的任务是检查第5个值,如果是5,例如,我想取第5个值=5的每一行,并为它们创建一个csv,如果5thvalue=10,我想为这些记录创建一个csv,等等。但是一次只能执行一个任务,我如何访问这些值?
2条答案
按热度按时间qq24tv8q1#
1:行尾逗号表示第一项为空
""
2:您可以得到第5个值,如下所示:
3:然后可以获得值为
fiveIndex
的行列表在您的示例中,第3行和第5行的值为10(index=2,index=4)。然后,您可以将这些行保存在
csv file
中。7qhs6swi2#
最后做了:
使命就完成了。