Having issues with CSV files with columns that should be simply ignored, irregulat formating

52 Views Asked by At

We have a series of datafiles that are a rectangle of data that someone exports from Excel. Sometimes they export extra columns that are blank entirely. 1, 2, 15. We don't want those columns OR their content, which are nominally empty.

I.E. (pseudo codee)

Our object model

class object "Address" {
Name as string
Street as string
City as string
State as string
Zip as string}
Name, Street, City, State, Zip,,,,,
Bob, Windsor, Chicago, IL, 12342,,,,,
Tom, Second, St Louis, MO, 45122,,,,,
Steve, Main, Nashville, TN, 12124,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,

We want the 3 rows of five columns of data. We can add new items to the class for the four "unnecessary" columns, but that doesn't work when the number of those erroneous columns is variable or unknown. When we ignore and continue we get no data. When we don't turn off errors it gets upset when we don't have object class items for those columns.

etc.

We are expecting it to only read columns as we defined in our object model and ignore any extraneous columns

1

There are 1 best solutions below

0
shamp00 On

Put an array field at the end of the class and decorate it with [FieldOptional].

Here is a working program:

[DelimitedRecord(",")]
public class Contact
{
    [FieldTrim(TrimMode.Both)]
    public string Name { get; set; }
    [FieldTrim(TrimMode.Both)]
    public string Street { get; set; }
    [FieldTrim(TrimMode.Both)]
    public string City { get; set; }
    [FieldTrim(TrimMode.Both)]
    public string State { get; set; }
    [FieldTrim(TrimMode.Both)]
    public string Zip { get; set; }

    [FieldOptional]
    public string[] Ignore { get; set; }
}

internal class Program
{
    static void Main(string[] args)
    {
        var engine = new DelimitedFileEngine<Contact>();
        var records = engine.ReadString("Bob, Windsor, Chicago, IL, 12342,,,,,");

        Assert.AreEqual("Bob", records[0].Name);
        Assert.AreEqual("Windsor", records[0].Street);
        Assert.AreEqual("Chicago", records[0].City);
        Assert.AreEqual("IL", records[0].State);
        Assert.AreEqual("12342", records[0].Zip);

        Console.WriteLine("All OK");
        Console.ReadKey();
    }
}