我有一个包含以下数据的 csv 文件:
500000,0.005,6000
690000,0.003,5200
我需要将每一行添加为一个单独的数组。所以 50000、0.005、6000 将是 array1。我该怎么做?
目前我的代码将每一列添加到一个元素中。
例如 data[0] 显示 500000 690000
static void ReadFromFile(string filePath)
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filePath))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
string[] data = line.Split(',');
Console.WriteLine(data[0] + " " + data[1]);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
