The SDL Component Suite is an industry leading collection of components supporting scientific and engineering computing. Please visit the SDL Web site for more information....



ImportFromCSV


Unit:SDL_datatable
Class: TDataTable
Declaration: [1] function ImportFromCSV (FName: string; Delimiter: TCSVDelimiters; IncludeColHeaders, IncludeRowHeaders: boolean): integer;
[2] function ImportFromCSV (FName: string; Delimiter: TCSVDelimiters; NLinesSkip, LastImportLine: integer; IncludeColHeaders, IncludeRowHeaders: boolean): integer;

The method ImportFromCSV reads the CSV file (comma separated values file) FName. The parameter Delimiter determines the delimiting character used in the CSV file. Setting the Delimiter to csvUnknown forces the routine to determine the delimiting character automatically by counting the number of occurrences of the possible separator characters. The one with the highest rate of occurrence is considered to be the delimiting character. This approach should work in most of the cases. If not, you can specify a particular delimiting character by setting the Delimiter parameter to the corresponding value.

If the parameter IncludeColHeaders is set to TRUE the first row is interpreted as the column headings, if IncludeRowHeaders is set to TRUE the first column of the data array is interpreted as row headings. The function returns a zero value if the CSV file has been read successfully.

Version [2] of ImportFromCSV allows to skip the first NLinesSkip lines of the CSV file and to stop at a specific line (parameter LastImportLine). These parameters can be used to import just a part of the CSV file. Set both parameters to zero if you do not want to use this feature.

The method ImportFromCSV assigns nominal values automatically whenever a non-numeric cell is read. However, this may result in inconsistencies if numeric and non-numeric cells are mixed within a particular column. Further, you should keep in mind that nominal and ordinal values cannot be distinguished by the import routine, thus the corresponding column is always set to the nominal scale type.

The method returns the following error codes:

 0 ... everything is OK
-1 ... too many nominal identifiers (warning only)
-2 ... at least one undefined cell (warning only)
-3 ... NLinesSkip and LastImportLine must not be negative
-4 ... LastImportLine must not be less than NSkipLine

Hint 1: Please note that the CSV format has never been standardized, and thus a plethora of different sub-versions exists. Further, the decimal separator and the thousand separator character change between different languages, contributing to even greater confusion. For example, in German implementations the comma is replaced by the semicolon in order to be able to use the comma as a decimal separator. Thus you should always be careful to select the proper delimiter, especially when writing software for multi-language environments.

Hint 2: ImportFromCSV triggers the OnPercentDone event to provide feedback during time consuming read operations.



Last Update: 2023-Dec-14