Parsing .csv file into 2d array

后端 未结 5 1481
我寻月下人不归
我寻月下人不归 2021-02-08 01:20

I\'m trying to parse a CSV file into a 2D array in C#. I\'m having a very strange issue, here is my code:

string filePath = @\"C:\\Users\\Matt\\Desktop\\Eve Spre         


        
5条回答
  •  离开以前
    2021-02-08 02:01

    Nothing in your code gets the number of lines out of your file in time to use it.

    Line.Length represents the number of columns in your csv, but it looks like you're also trying to use it to specify the number of lines in your file.

    This should get you your expected result:

    string filePath = @"C:\Users\Matt\Desktop\Eve Spread Sheet\Auto-Manufacture.csv";
    StreamReader sr = new StreamReader(filePath);
    var lines = new List();
    int Row = 0;
    while (!sr.EndOfStream)
    {
        string[] Line = sr.ReadLine().Split(',');
        lines.Add(Line);
        Row++;
        Console.WriteLine(Row);
    }
    
    var data = lines.ToArray();
    

提交回复
热议问题