Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FileHelpers without a type

I have a CSV file that is being exported from another system whereby the column orders and definitions may change. I have found that FileHelpers is perfect for reading csv files, but it seems you cannot use it unless you know the ordering of the columns before compiling the application. I want to know if its at all possible to use FileHelpers in a non-typed way. Currently I am using it to read the file but then everything else I am doing by hand, so I have a class:

[DelimitedRecord(",")]
public class CSVRow
{
    public string Content { get; set; }
}

Which means each row is within Content, which is fine, as I have then split the row etc, however I am now having issues with this method because of commas inherent within the file, so a line might be:

"something",,,,0,,1,,"something else","","",,,"something, else"

My simple split on commas on this string doesnt work as there is a comma in `"something, else" which gets split. Obviously here is where something like FileHelpers comes in real handy, parsing these values and taking the quote marks into consideration. So is it possible to use FileHelpers in this way, without having a known column definition, or at least being able to pass it a csv string and get a list of values back, or is there any good library that does this?

like image 702
Ryan Amies Avatar asked Oct 31 '12 15:10

Ryan Amies


1 Answers

You can use FileHelpers' RunTime records if you know (or can deduce) the order and definitions of the columns at runtime.

Otherwise, there are lots of questions about CSV libraries, eg Reading CSV files in C#

Edit: updated link. Original is archived here

like image 110
stuartd Avatar answered Oct 03 '22 05:10

stuartd