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 ?
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With