Class TCSVReader

Unit

Declaration

type TCSVReader = class(TObject)

Description

Reader for CSV files.

TCSVReader is the main class to read a CSV file row by row. There are two possible ways to read a row:

  1. Read the next row using ReadRow.

  2. Use the Rows enumerator to iterate over all rows.

In both cases the cells in each row are returned in their original order and are access with indices starting at 0.

If the CSV file has column headers in the first row, the cells in a row can be accessed using their names with ReadNamedRow or the or the NamedRows enumerator. The headers of the columns to be retrieved are passed to either method and the cells of the row are returned in the order of the headers.

Example:

program ReadCSV;
uses easycsv;
var
   csv : TCSVReader;
   row : TCSVReader.TRow;
   i : Integer;
begin
   csv := TCSVReader.Create('your-file.csv');
   // Just show the csv file
   for row in csv do begin
      for i := 0 to row.Count - 1 do begin
         if i > 0 then write(' ');
         write(row[i]);
      end;
      writeln;
   end;
   csv.Free;
end.

See the corresponding methods for some examples.

Hierarchy

Overview

Nested Classes and Records

Public TNamedEnum = record
Public TRow = record
Public TRowEnum = record

Methods

Public constructor Create;
Public constructor Create(stream : TStream);
Public constructor Create(filename : String);
Public destructor Destroy; override;
Public function IndexOfHeader(header : String) : Integer;
Public function ReadRow(out row : TRow) : Boolean;
Public function ReadRow(out elems : TStringArray) : Boolean;
Public function ReadNamedRow(out row : TRow) : Boolean;
Public function ReadNamedRow(out elems : TStringArray) : Boolean;
Public function Rows: TRowEnum;
Public function GetEnumerator: TRowEnum; inline;
Public function NamedRows: TNamedEnum;
Public function NamedRows(names : array of String) : TNamedEnum;

Properties

Public property SourceText : String write SetSourceText;
Public property Source : TStream write SetSource;
Public property SourceFile : String write SetSourceFile;
Public property HasHeader : Boolean read FparseHeaders write FparseHeaders;
Public property NumHeaders : Integer read GetNumHeaders;
Public property Header[col:Integer]: String read GetHeader;
Public property Headers : TStringArray read GetHeaders;
Public property NamedHeaders : TStringArray read FnamedHeaders write SetNamedHeaders;
Public property Delimiter : char read Fdelimiter write Fdelimiter;
Public property QuoteChar : char read FquoteChar write FquoteChar;
Public property LineEnding : char read FlineEnding write FlineEnding;
Public property IgnoreOuterWhitespace : Boolean read FignoreOuterWhitespace write FignoreOuterWhitespace;
Public property MinColumns : Integer read FminNumCols write FminBndCols;
Public property MaxColumns : Integer read FmaxNumCols write FmaxBndCols;
Public property NumColumns : Integer write SetMinMaxColumns;
Public property NumRows : Integer read GetNumRows;
Public property MaxBufferSize : Integer read FmaxBufSize write FmaxBufSize;
Public property OwnsStream : Boolean read FownsStream write FownsStream;
Public property CodePage : TSystemCodePage read FcodePage write FcodePage;

Description

Methods

Public constructor Create;

Create a new uninitialized CSV reader.

Public constructor Create(stream : TStream);

Create a CSV reader reading from the given input stream.

Public constructor Create(filename : String);

Create a CSV reader reading from the given named file.

Public destructor Destroy; override;
 
Public function IndexOfHeader(header : String) : Integer;

Return the index of a column with the given header.

If there is no column with that header, the function returns - 1.

Public function ReadRow(out row : TRow) : Boolean;

Read the next row.

Example:

var
  row : TCSVReader.TRow;
  i : Integer;
begin
   ...
   while reader.ReadRow(row) do begin
     for i := 0 to row.Count do write(row[i], ' ');
     writeln;
   end
end

Parameters
row
the next row
Returns

True if and only if there is a next row and False at EOF

Public function ReadRow(out elems : TStringArray) : Boolean;

Read the next row as string array.

Example:

var
  row : array of String;
  i : Integer;
begin
   ...
   while reader.ReadRow(row) do begin
     for i := 0 to High(row) do write(row[i], ' ');
     writeln;
   end
end

Parameters
elems
the next row
Returns

True if and only if there is a next row and False at EOF

Public function ReadNamedRow(out row : TRow) : Boolean;

Read the next row in order of the requested headers.

The requested headers are set by the NamedColumns property. Only the elements according to these headers are returned in row in order of the headers.

Example:

var
  row : TCSVReader.TRow;
begin
   ...
   reader.NamedHeaders := ['Name', 'First Name'];
   while reader.ReadRow(row) do begin
     write('Name: ', row[0]);
     write('First Name: ', row[1]);
   end
end

Parameters
elems
the next row
Returns

True if and only if there is a next row and False at EOF

Public function ReadNamedRow(out elems : TStringArray) : Boolean;

Read the next row as string array in order of the requested headers.

The requested headers are set by the NamedColumns property. Only the elements according to these headers are returned in row in order of the headers.

Example:

var
  row : array of String;
  i : Integer;
begin
   ...
   reader.NamedHeaders := ['Name', 'First Name'];
   while reader.ReadRow(row) do begin
     write('Name: ', row[0]);
     write('First Name: ', row[1]);
   end
end

Public function Rows: TRowEnum;

Enumerate all rows

Example:

var
  row : TCSVReader.TRow;
  i : Integer;
begin
   ...
   for row in reader.Rows do begin
     for i := 0 to High(row) do write(row[i], ' ');
     writeln;
   end
end

Public function GetEnumerator: TRowEnum; inline;

Return the row enumerator.

Public function NamedRows: TNamedEnum;

Return an enumerator retrieving the columns with the specified NamedHeaders.

Example:

var
  row : TCSVReader.TRow;
begin
   ...
   reader.NamedHeaders := ['Name', 'First Name'];
   for row in reader.NamedRows do begin
     write('Name: ', row[0]);
     write('First Name: ', row[1]);
   end
end

Public function NamedRows(names : array of String) : TNamedEnum;

Return an enumerator retrieving the columns with the given header names.

This function basically sets NamedHeaders to names and the calls NamedRows.

Example:

var
  row : TCSVReader.TRow;
begin
   ...
   for row in reader.NamedRows(['Name', 'First Name']) do begin
     write('Name: ', row[0]);
     write('First Name: ', row[1]);
   end
end

Properties

Public property SourceText : String write SetSourceText;

Set the CSV input text as a string.

The reader is reset to parse the given string.

The codepage is changed to the string's codepage.

Public property Source : TStream write SetSource;

Set the CSV input stream.

The reader is reset to parse CSV data from the given stream.

Public property SourceFile : String write SetSourceFile;

Set the CSV input file.

The reader is reset to parse CSV data from the given named file.

Public property HasHeader : Boolean read FparseHeaders write FparseHeaders;

Whether the first row of CSV file should be parsed as column headers.

Public property NumHeaders : Integer read GetNumHeaders;

The number of headers, i.e. the number of cells in the first row.

Public property Header[col:Integer]: String read GetHeader;

The headers, i.e. the contents of the fields in the first row.

Public property Headers : TStringArray read GetHeaders;

The headers, i.e. the contents of the fields in the first row, as array.

Public property NamedHeaders : TStringArray read FnamedHeaders write SetNamedHeaders;

The header names to be retrieved in order.

The elements corresponding to the specified headers in this order can be retrieved by ReadNamedRow or NamedRows.

Because retrieving named headers requires headers, setting this property also enables HasHeader.

Public property Delimiter : char read Fdelimiter write Fdelimiter;

The separator of cells in a row (default: DefaultDelimiter)

Public property QuoteChar : char read FquoteChar write FquoteChar;

The quote character for each cell (default: DefaultQuoteChar)

Public property LineEnding : char read FlineEnding write FlineEnding;

The delimiter of a row (default: DefaultLineEnding)

Public property IgnoreOuterWhitespace : Boolean read FignoreOuterWhitespace write FignoreOuterWhitespace;

Whether spaces at the beginning and end of each cell should be removed (default: DefaultIgnoreOuterWhitespace).

Public property MinColumns : Integer read FminNumCols write FminBndCols;

The minimal number of columns in a row read so far.

When set each row must have at least this number of rows, otherwise an ECSVReadError is raised.

Public property MaxColumns : Integer read FmaxNumCols write FmaxBndCols;

The maximal number of columns in a row read so far.

When set each row must have at most this number of rows, otherwise an ECSVReadError is raised.

Public property NumColumns : Integer write SetMinMaxColumns;

Set the minimal and maximal number of columns.

Public property NumRows : Integer read GetNumRows;

The number of rows read so far.

Public property MaxBufferSize : Integer read FmaxBufSize write FmaxBufSize;

The maximal size of the internal row buffer

Public property OwnsStream : Boolean read FownsStream write FownsStream;

Whether this reader owns the input stream

If True the input stream will be deleted if this reader is deleted.

Public property CodePage : TSystemCodePage read FcodePage write FcodePage;

Set the codepage of the input file or stream.

The default is the system's default codepage DefaultSystemCodePage.


Generated by PasDoc 0.15.0.