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:
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
- TObject
- TCSVReader
Overview
Nested Classes and Records
TNamedEnum = record |
|
TRow = record |
|
TRowEnum = record |
Methods
constructor Create; |
|
constructor Create(stream : TStream); |
|
constructor Create(filename : String); |
|
destructor Destroy; override; |
|
function IndexOfHeader(header : String) : Integer; |
|
function ReadRow(out row : TRow) : Boolean; |
|
function ReadRow(out elems : TStringArray) : Boolean; |
|
function ReadNamedRow(out row : TRow) : Boolean; |
|
function ReadNamedRow(out elems : TStringArray) : Boolean; |
|
function Rows: TRowEnum; |
|
function GetEnumerator: TRowEnum; inline; |
|
function NamedRows: TNamedEnum; |
|
function NamedRows(names : array of String) : TNamedEnum; |
Properties
property SourceText : String write SetSourceText; |
|
property Source : TStream write SetSource; |
|
property SourceFile : String write SetSourceFile; |
|
property HasHeader : Boolean read FparseHeaders write FparseHeaders; |
|
property NumHeaders : Integer read GetNumHeaders; |
|
property Header[col:Integer]: String read GetHeader; |
|
property Headers : TStringArray read GetHeaders; |
|
property NamedHeaders : TStringArray read FnamedHeaders write SetNamedHeaders; |
|
property Delimiter : char read Fdelimiter write Fdelimiter; |
|
property QuoteChar : char read FquoteChar write FquoteChar; |
|
property LineEnding : char read FlineEnding write FlineEnding; |
|
property IgnoreOuterWhitespace : Boolean read FignoreOuterWhitespace write FignoreOuterWhitespace; |
|
property MinColumns : Integer read FminNumCols write FminBndCols; |
|
property MaxColumns : Integer read FmaxNumCols write FmaxBndCols; |
|
property NumColumns : Integer write SetMinMaxColumns; |
|
property NumRows : Integer read GetNumRows; |
|
property MaxBufferSize : Integer read FmaxBufSize write FmaxBufSize; |
|
property OwnsStream : Boolean read FownsStream write FownsStream; |
|
property CodePage : TSystemCodePage read FcodePage write FcodePage; |
Description
Methods
constructor Create; |
|
Create a new uninitialized CSV reader. |
constructor Create(stream : TStream); |
|
Create a CSV reader reading from the given input stream. |
constructor Create(filename : String); |
|
|
destructor Destroy; override; |
|
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. |
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
Returns
|
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
Returns
|
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 |
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 |
function GetEnumerator: TRowEnum; inline; |
|
Return the row enumerator. |
function NamedRows: TNamedEnum; |
|
Return an enumerator retrieving the columns with the specified 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 |
function NamedRows(names : array of String) : TNamedEnum; |
|
Return an enumerator retrieving the columns with the given header This function basically sets 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
property SourceText : String write SetSourceText; |
|
Set the CSV input text as a string. The reader is reset to parse the given string. |
property Source : TStream write SetSource; |
|
Set the CSV input stream. The reader is reset to parse CSV data from the given stream. |
property SourceFile : String write SetSourceFile; |
|
Set the CSV input file. The reader is reset to parse CSV data from the given named file. |
property HasHeader : Boolean read FparseHeaders write FparseHeaders; |
|
Whether the first row of CSV file should be parsed as column headers. |
property NumHeaders : Integer read GetNumHeaders; |
|
The number of headers, i.e. the number of cells in the first row. |
property Header[col:Integer]: String read GetHeader; |
|
The headers, i.e. the contents of the fields in the first row. |
property Headers : TStringArray read GetHeaders; |
|
The |
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. |
property Delimiter : char read Fdelimiter write Fdelimiter; |
|
The separator of cells in a row (default: DefaultDelimiter) |
property QuoteChar : char read FquoteChar write FquoteChar; |
|
The quote character for each cell (default: DefaultQuoteChar) |
property LineEnding : char read FlineEnding write FlineEnding; |
|
The delimiter of a row (default: DefaultLineEnding) |
property IgnoreOuterWhitespace : Boolean read FignoreOuterWhitespace write FignoreOuterWhitespace; |
|
Whether spaces at the beginning and end of each cell should be removed (default: DefaultIgnoreOuterWhitespace). |
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. |
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. |
property NumColumns : Integer write SetMinMaxColumns; |
|
Set the minimal and maximal number of columns. |
property NumRows : Integer read GetNumRows; |
|
The number of rows read so far. |
property MaxBufferSize : Integer read FmaxBufSize write FmaxBufSize; |
|
The maximal size of the internal row buffer |
property OwnsStream : Boolean read FownsStream write FownsStream; |
|
Whether this reader owns the input stream If |
property CodePage : TSystemCodePage read FcodePage write FcodePage; |
|
Set the The default is the system's default |
Generated by PasDoc 0.15.0.