Parsing .csv file into 2d array

后端 未结 5 1494
我寻月下人不归
我寻月下人不归 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条回答
  •  猫巷女王i
    2021-02-08 02:09

    A shorter version of the code above:

    var filePath = @"C:\Users\Matt\Desktop\Eve Spread Sheet\Auto-Manufacture.csv";
    var data = File.ReadLines(filePath).Select(x => x.Split(',')).ToArray();
    

    Note the user of ReadLines instead of ReadAllLines, which is more efficient on larger files as per MSDN documentation:

    When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.

提交回复
热议问题