Reputation: 2225
I need to parse CSV file, get specific columns, and convert values to double. My code below works fine if all values can be converted to double :). But how should I update the code if value will be equal "TRUE"/"FALSE" ?
Code:
dt = GetDataTableFromCsv("my_csv.csv");
// Gets the column of the dependent/indepent variable
// I need structure double [][]
var input = dt.AsEnumerable().Select(r => independentNames.Where(i => dt.Columns.Contains(i)).Select(c => Convert.ToDouble(r.Field<object>(c), provider(???))).ToArray()).ToArray();
Example of CSV:
Rank, Value, TV, IS_NEW, IS_HOME, RATINGS, ...
"1", "0.5", "CNN", "TRUE", "FALSE", "888.77" ....
independentNames
Upvotes: 0
Views: 247
Reputation: 21947
If all you need to do is wrap "TRUE"
and "FALSE"
to 1.0
and 0.0
, separate the logic to your own method and call it instead of Convert.ToDouble
.
You will still need to handle invalid inputs, either in this method or at a higher scope.
double AsDouble(string input)
{
switch (input)
{
case "TRUE": return 1.0;
case "FALSE": return 0.0;
//any other special cases
default: return Convert.ToDouble(input); //may still throw!
}
}
Upvotes: 2