用C#把csv文件转成list
以下是用C#将CSV文件转换为List的示例代码:
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = "example.csv";
List<string[]> data = new List<string[]>();
using (StreamReader reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] values = line.Split(',');
data.Add(values);
}
}
// 输出List中的数据
foreach (string[] row in data)
{
Console.WriteLine(string.Join(", ", row));
}
Console.ReadKey();
}
}
在此示例中,我们首先定义了要读取的CSV文件的路径,并创建了一个List来容纳CSV文件中的数据。然后,我们使用StreamReader逐行读取CSV文件,并使用字符串Split方法将每行数据拆分为字符串数组。最后,我们将每个字符串数组添加到List中。
请注意,此示例假设CSV文件中的每行包含相同数量的逗号分隔值。如果CSV文件中的某些行包含不同数量的分隔值,则需要根据您的具体情况进行修改
原文地址: https://www.cveoy.top/t/topic/hddE 著作权归作者所有。请勿转载和采集!