the CsvFileIterator class allows CSV files to be iterated
CsvFileIterator Constructor Option Hash Overview
The CsvFileIterator class constructor takes an optional hash with possible keys given in the following table. Note that key names are case-sensitive, and data types are soft (conversions are made when possible).
CsvFileIterator Options
Option | Data Type | Description |
"encoding" | String | the character encoding for the file (and for tagging string data read); if the value of this key is not a string then it will be ignored |
"separator" | String | the string separating the fields in the file (default: "," ) |
"quote" | String | the field quote character (default: '"' ) |
"eol" | String | the end of line character(s) (default: auto-detect); if the value of this key is not a string then it will be ignored |
"ignore-empty" | Boolean | if True (the default) then empty lines will be ignored; this option is processed with parse_boolean() |
"ignore-whitespace" | Boolean | if True (the default) then leading and trailing whitespace will be stripped from non-quoted fields; this option is processed with parse_boolean() |
"header-names" | Boolean | if True then the object will parse the header names from the first header row, in this case "header-lines" must be > 0 |
"header-lines" | Integer | the number of headers lines in the file (must be > 0 if "header-names" is True) |
"headers" | List of strings | list of header / column names for the data iterated, if this is present, then "header-names" must be False |
"verify-columns" | Boolean | if True (the default is False) then if a line is parsed with a different column count than other lines, a CSVFILEITERATOR-DATA-ERROR exception is thrown |
"fields" | Hash | the keys are column names (or numbers in case column names are not used) and the values are either strings (one of Option Field Types giving the data type for the field) or a Option Field Hash describing the field |
"timezone" | String | the timezone to use when parsing dates (will be passed to Qore::TimeZone::constructor()) |
Option Field Types
CsvFileIterator Option Field Types
Name | Description |
"int" | the value will be unconditionally converted to an integer using the Qore::int() function |
"float" | the value will be unconditionally converted to a floating-point value using the Qore::float() function |
"number" | the value will be unconditionally converted to an arbitrary-precision number value using the Qore::number() function |
"string" | (the default) the value remains a string; no transformation is done on the input data |
"date" | in this case dates are parsed directly with the Qore::date() function (and therefore are tagged automatically with the current time zone); to specify another date format, use the hash format documented below |
Option Field Hash
See here for an example of using the hash field description in the constructor().
CsvFileIterator Option Field Hash
Key | Value Description |
"type" | one of the option type values giving the field type |
"format" | used only with the "date" type; this is a date/time format mask for parsing dates |
"timezone" | used only with the "date" type; this value is passed to Qore::TimeZone::constructor() and the resulting timezone is used to parse the date (this value overrides any default time zone for the object; use only in the rare case that date/time values from different time zones are present in different columns of the same file) |
"code" | this is a Closures or Call References that takes a single argument of the value (after formatting with any optional "type" formats) and returns the value that will be output for the field |