Reading only specific columns from a CSV file out of many

前端 未结 3 1082
执念已碎
执念已碎 2020-12-10 20:24

I have a CSV file which looks like this basically:

TransactionID         ProfileID       Date           // more columns here...
somevalue             1231231         


        
3条回答
  •  暖寄归人
    2020-12-10 21:00

    The header is still to skip in this code, but with this code, you can choose which columns to extract. It might be much faster if you use a StreamReader. And you will need a constructor for your object.

    var temp = File.ReadAllLines(@"C:\myFile.csv");
    public List() myExtraction = new List();
    foreach(string line in temp)
    {
      var delimitedLine = line.Split('\t'); //set ur separator, in this case tab
    
      myExtraction.Add(new MyMappedCSVFile(delimitedLine[0], delimitedLine[3]));
    }
    

    Code for your Object:

    public class MyMappedCSVFile
    {
      public string ProfileID { get; set; } 
      public string Date { get; set; }
    
      public MyMappedCSVFile(string profile, string date)
      {
        ProfileID = profile;
        Date = date;
      }
    }
    

提交回复
热议问题