FileHelpers icon indicating copy to clipboard operation
FileHelpers copied to clipboard

Add option to ignore unmapped columns when reading a file

Open schuettecarsten opened this issue 5 years ago • 2 comments

I have a CSV file that looks like this:

"RECHNUNG";"DATUM";"KUNDENNR";"DEBITORKONTO" "12345";"04.08.2020 00:00:00";"12345";"" "12345";"04.08.2020 00:00:00";"12345";"" "12345";"04.08.2020 00:00:00";"12345";""

If my class only contains three or less properties, I cannot read the CSV file. Please add an option that makes the parser ignore all unmapped columns when reading CSV files.

schuettecarsten avatar Jan 05 '21 14:01 schuettecarsten

Hi Carsten

Could you please add how you use FileHelpers to read this file?

Thank you, Matthias

mcavigelli avatar Feb 05 '21 22:02 mcavigelli

That's quite easy:

var dataEngine = new FileHelperEngine<T>();
dataEngine.ErrorMode = ErrorMode.SaveAndContinue;
var items = readItems(dataEngine);

The class that is used for <T> is like this:

[DelimitedRecord(";")]
[IgnoreFirst(1)]
[IgnoreEmptyLines]
internal sealed class CsvFileItem
{
	[FieldQuoted('"', QuoteMode.OptionalForBoth)]
	public string Rechnung { get; set; }

	[FieldQuoted('"', QuoteMode.OptionalForBoth)]
	public string Datum { get; set; }

	[FieldQuoted('"', QuoteMode.OptionalForBoth)]
	public string KundenNr { get; set; }

	[FieldQuoted('"', QuoteMode.OptionalForBoth)]
	public string DebitorKonto { get; set; }
}

Sometimes, re receive CSv files that have additional colums - not at the end, but in the middle of the file. At the moment, FileHelpers simply maps the first CSV file field to the first property in the class. I need a solution to modify this mapping, ignore fields in the CSV file and for example map the first CSV field to the third class property and so on...

schuettecarsten avatar Jan 28 '22 08:01 schuettecarsten

@schuettecarsten I believe ignoring column data at runtime will not be possible, What will be the criteria for it ? Is that need to present in data file ?

I believe instead of that, What you are looking for is headers in the file should match column names in model and decide its field order.

Example 1, if below file is give as a input

"RECHNUNG";"DATUM";"KUNDENNR";"DEBITORKONTO" "12345";"04.08.2020 00:00:00";"12345";""; "12345";"04.08.2020 00:00:00";"12345";"" "12345";"04.08.2020 00:00:00";"12345";""

Then we should have attribute [MatchHeadersToModel] on model class should result in field order in model as "RECHNUNG" : 1 "DATUM": 2 "KUNDENNR" : 3 "DEBITORKONTO" : 4

Example 2., if below file is give as a input

"DATUM";"RECHNUNG";"KUNDENNR";"DEBITORKONTO" "04.08.2020 00:00:00";"12345";"12345";""; "04.08.2020 00:00:00";"12345";"12345";"" "04.08.2020 00:00:00";"12345";"12345";""

Then we should have attribute [MatchHeadersToModel] on model class should result in field order in model as "DATUM": 1 "RECHNUNG" : 2 "KUNDENNR" : 3 "DEBITORKONTO" : 4

So, basically, we are reading header in file and then matching the model with headers and their order, instead of statically defining in file.

@mcavigelli Is that possible to achieve ?

sandip4git avatar Jan 20 '24 15:01 sandip4git

As this ticket is over 2 years old and was never answered, I've created my own CSV reader that fits my needs.

schuettecarsten avatar Jan 20 '24 15:01 schuettecarsten