Search code examples
c#csvfile.readalllines

Proof Reading .CSV per line


CSVHelper and FileHelper is not an option

I have a .csv export that I need to check for consistency structured like the below

Reference,Date,EntryID

ABC123,08/09/2015,123

ABD234,08/09/2015,124

XYZ987,07/09/2015,125

QWE456,08/09/2016,126

I can use ReadLine or RealAllLines and .Split which give me entire rows/columns BUT I have need to select each row and then go through each attribute (separated by ',') for format checking

I am running into problems here. I can not single out each value in a row for this check. It is probably either something simple onto

 class Program
{
    static void Main(string[] args)
    {
        string csvFile = @"proof.csv";
        string[] lines = File.ReadAllLines(csvFile);


        var values = lines.Skip(1).Select(l => new { FirstRow = l.Split('\n').First(), Values = l.Split('\n').Select(v => int.Parse(v)) });
        foreach (var value in values)
        {
            Console.WriteLine(string.Format("{0}", value.FirstRow));
        }
    }
}

Or I am going down the wrong path, my searches relate to pulling specific rows or columns (as opposed to checking the individual values associated)

The sample of the data above has a highlighted example: The date is next year and I would like to be able to proof that value (just an example as it could be in either column where errors appear)


Solution

  • I can not single out each value in a row

    That's because you split on \n twice. The values within a row are separated by comma (,).

    I'm not sure what all that LINQ is supposed to do, but it's as simple as this:

    string[] lines = File.ReadAllLines(csvFile);
    
    foreach (var line in lines.Skip(1))
    {
        var values = line.Split(',');
        // access values[0], values[1] ...
    }