Friday, September 8, 2023

csv header check

static void checkHeaders()
    {
        string[] providedHeaders = 
        {
            "Title", "Document Type", "Modified", "Department", "Authors", "Url",
            "Country", "City", "Subdivision", "File Type", "Product Type", "Language",
            "Index Number", "Inspection Type", "Account Number", "Business Title",
            "Visit Type", "Filename"
        };

        string csvPath = "/path/to/your/csv/file.csv"; // Replace with your actual file path
        
        // Read the first line to get the headers
        string firstLine = File.ReadLines(csvPath).FirstOrDefault();
        
        char detectedDelimiter = new[] { ';', ',', '\t' }.OrderByDescending(d => firstLine.Count(c => c == d)).First();

        // Split the headers using the detected delimiter and trim spaces
        string[] csvHeaders = firstLine.Split(detectedDelimiter).Select(h => h.Trim()).ToArray();

        // Assert that headers match
        Debug.Assert(csvHeaders.SequenceEqual(providedHeaders), "Headers do not match!");
        Console.WriteLine("Headers match successfully!");
    }

No comments:

Post a Comment