CsvOpenReadAnalyzed Method

Opens a CSV file for reading after it had been analyzed.

Definition

Namespace: FolkerKinzel.CsvTools
Assembly: FolkerKinzel.CsvTools (in FolkerKinzel.CsvTools.dll) Version: 2.0.1+2345335399184346d9b2cc992ed5c814406052c1
C#
public static CsvReader OpenReadAnalyzed(
	string filePath,
	Encoding? defaultEncoding = null,
	Header header = Header.ProbablyPresent,
	bool disableCaching = false,
	int analyzedLines = 5
)

Parameters

filePath  String
File path of the CSV file.
defaultEncoding  Encoding  (Optional)
The text Encoding to be used if the CSV file has no byte order mark (BOM), or null to use UTF8 in this case. Use GetExcelArguments to get the appropriate argument for this parameter when importing CSV data from Excel.
header  Header  (Optional)
A supposition that is made about the presence of a header row.
disableCaching  Boolean  (Optional)
true to set the DisableCaching flag, otherwise false.
analyzedLines  Int32  (Optional)
Maximum number of lines to analyze in the CSV file. The minimum value is AnalyzedLinesMinCount. If the file has fewer lines than analyzedLines, it will be analyzed completely. (You can specify Int32.MaxValue to analyze the entire file in any case.)

Return Value

CsvReader
A CsvReader that allows to iterate the data.

Remarks

The method performs a statistical analysis on the CSV file to find the appropriate parameters for reading the file. The result of the analysis is therefore always only an estimate, the accuracy of which increases with the number of lines analyzed. The analysis is time-consuming because the CSV file has to be accessed for reading.

The field delimiters COMMA (',', %x2C), SEMICOLON (';', %x3B), HASH ('#', %x23), TAB ('\t', %x09), and SPACE (' ', %x20) are recognized automatically.

This method also tries to determine the Encoding of the CSV file from the byte order mark (BOM). If no byte order mark can be found, defaultEncoding is used.

Example

  Note

In the following code examples - for easier readability - exception handling has been omitted.
C#
using System.Text;
using FolkerKinzel.CsvTools;

namespace Examples;

internal static class CsvAnalyzerExample
{
    public static void ParseForeignCsvFile(string filePath)
    {
        const string nonStandardCsv = """


            First # "Second # Column"   
            1,"2",3 # "Get's
            too much" # LOST?

            too few


            """;

        File.WriteAllText(filePath, nonStandardCsv, Encoding.Unicode);

        using CsvReader csv = Csv.OpenReadAnalyzed(filePath);
        CsvRecord[] data = [.. csv];

        using (CsvWriter writer = Csv.OpenWrite(filePath, data[0].ColumnNames))
        {
            foreach (CsvRecord record in data)
            {
                writer.Record.FillWith(record.Values);
                writer.WriteRecord();
            }
        }

        Console.WriteLine(File.ReadAllText(filePath));
    }
}

/*
 Console output:

Column1,Column2,Column3
First ,Second # Column,
"1,""2"",3 ","Get's
too much", LOST?
too few,,

*/

Exceptions

ArgumentNullExceptionfilePath is null.
ArgumentOutOfRangeException

header is not a defined value of the Header enum.

- or -

header is a combination of Header values.

ArgumentExceptionfilePath is not a valid file path.
IOExceptionI/O error.

See Also