Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading only specific columns from a CSV file out of many

I have a CSV file which looks like this basically:

TransactionID         ProfileID       Date           // more columns here...
somevalue             123123123      somedate
somevalue             123123123      somedate              
somevalue             123123123      somedate
somevalue             123123123      somedate
somevalue             123123123      somedate

I would like to extract only specific columns which are ProfileID and Date out of all these columns from the CSV file, either by mapping it to an existing class like this:

public class MyMappedCSVFile
{
  public string ProfileID { get; set; } 
  public string Date { get; set; }
}

Or by storing it in a Dictionary collection?

I have tried something like this:

   public static List<string> ReadInCSV(string absolutePath)
        {
            List<string> result = new List<string>();
            string value;
            using (TextReader fileReader = File.OpenText(absolutePath))
            {
                var csv = new CsvReader(fileReader);
                csv.Configuration.HasHeaderRecord = false;
                while (csv.Read())
                {
                    for (int i = 0; csv.TryGetField<string>(i, out value); i++)
                    {
                        result.Add(value);
                    }
                }
            }
            return result;
        }

But this takes out everything out of the CSV file, literally everything in a single List, and it's not what I want...

Can someone help me out with this ?

like image 813
User987 Avatar asked Mar 07 '23 18:03

User987


1 Answers

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<MyMappedCSVFile>() myExtraction = new List<MyMappedCSVFile>();
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;
  }
}
like image 131
kurdy Avatar answered Apr 28 '23 16:04

kurdy