ParseCSV
new to Analytica 5.0
ParseCSV( csvText, columnIndex, rowIndex, separator, firstLineIsHeader, trimCells, columnsToKeep, rowIndexLabelColumn, parseFlags, dateTemplate, decimal, quote )
Parses text that is in a CSV-format (Comma Separated Values format) and returns a two-dimensional array containing the data. The CSV format in inherently two-dimensional, with items on each row separated by a comma or other specified «separator», and each row on a new line (separated by new-line characters, CR, LF or CRLF). Numerical value or dates are automatically parsed as appropriate.
Indexes for result
If you give it only the single text parameter, it uses the most common CSV conventions in the same style as Excel -- including commas to separate columns, and newline to separate rows, and double quotes to enclose text that may contain commas or newlines. It creates local indexes, Column
and Row
. Specify «columnIndex» and «rowIndex» if you want to use existing indexes for the result. If «columnIndex» has fewer columns or «rowIndex» has fewer rows than the data, it ignores the additional columns or rows in the data. If «columnIndex» has more columns or «rowIndex» has more rows than the data, it pads the extra columns with Null.
Non-comma separator
If values are delineated be something other than a comma, specify «separator». Commonly occurring non-comma separators include '|'
, Chr(9)
(tab), ';'
. «Separator» can be multiple characters.
Header column in data
In some CSV data files, the first line contains columns names. When you set «firstLineIsHeader» to true
, the values in the first line are used to construct the local column index (if you didn't specify «columnIndex»), and to identify specify columns when you've specified «columnsToKeep» or «rowIndexLabelsColumn», but the first line data does not appear in the resulting array itself. When «firstLineIsHeader» is set to false, then the first line is included as part of the array data, and not used to name the columns. When left unspecified, the value is inferred based on the other options specified, defaulting to true when the column names are used or referenced (i.e., when a local column index is created automatically, or «columnsToKeep» or «rowIndexLabelsColumn» are specified).
Spaces around values
«trimCells» controls whether leading or trailing space around each value are included or not. This defaults to true (surrounding space is not included). Spaces inside quotes are never trimmed.
Subset of or re-ordered columns
The «columnsToKeep» parameter can be used to select only a subset of the columns appearing in the CSV, or to re-order the columns.
If you extracting a single column only, then you can omit «columnIndex» and specify either the column header (from the first line) or the column number that you want to keep to «columnsToKeep».
If you are extracting more than one column, then you should specify «columnIndex» and «columnsToKeep» should be indexed by «columnIndex». When the elements of «columnIndex» already contains the column labels, you should pass the «columnIndex» to «columnsToKeep», e.g., ParseCSV(csv, col, columnsToKeep:col)
.
If you pass a list or array to «columnsToKeep», but don't specify «columnIndex», it will still extract the specified columns, but not as efficiently. In this case, array abstraction repeats the call for each element in the array passed to «columnsToKeep», parsing the «csvText» anew each time.
Using a column for row labels
You can use «rowIndexLabelsColumn» specify a column to be used for the row index labels, which a local row index is automatically created for you. The column used is removed from the array (unless you specify «columnIndex» with «matchColumnLabels» and explicitly include that column). For example, ParseCSV(csvText, rowIndexLabelsColumn:1)
treats the first column as row index labels rather than as array cells.
Parse flags
«parseFlags» is a bit-field of flags that control things such as for which cells an attempt is made to parse the cell into a number or date. The default (0) corresponds to the standard Excel conventions. The flags can be added, and are as follows.
- 0 = Parse both quoted ("52") and unquoted (52) cells.
- 1 = Don't parse any cells. E.g.,
="52"
,"52"
and52
will all be return as text. - 2 = Don't parse quoted cells. E.g.,
"52"
is text, but52
is numeric. - 4 = Disable the '=' prefix (
="0042"
normally suppressed parsing). - 8 = Recognize backslash-escaped quotes inside quotes. (in addition to the doubling of quotes, which is the usual CSV standard).
- 16 = Disable generalized date parsing. When on, only the format
6-May-2016
is recognized as a date but not other formats. This flag dramatically speeds up parsing times for data with no other date formats.
Date Formats
Use the «dateTemplate» parameter to specify the international ordering that should be used for parsing dates. Use the letters "d", "M" and "y" to specify the ordering of these components. For example, with a «dateTemplate» of "d/M/y", the text "11/10/9" parses as 11-Oct-2009, whereas with a «dateTemplate» of "y/M/d" it would be 9-Oct-2011, and with "M/d/y" it would be 10-Nov-2009.
This parameter uses the same conventions used in the Custom date formats template in the Number Format Dialog, but only the relative ordering of the day, month and year patterns matter. Hence, all these work equivalently: "dMy", "dd/MM/yyyy", "d-M-yy", "d M yyyy". Also, upper/lower case matters! "M" must be capital (because "m" means minutes").
Decimal point and thousands separators
Use the «decimal» parameter to specify the character used to separate the whole number from the fractional part of a number. Only two values are allowed: '.'
(dot) or ','
(comma). The default when omitted is the United States convention of '.'
.
When comma is used for «decimal», you will usually specify «separator» to be something other than comma, with semi-colon ';'
being a common convention. They aren't required to be different, but numbers containing decimals will need to be contained in quotes, otherwise the comma will be treated as the column separator.
When «decimal» is omitted or dot, then comma can be used to group digits, such as "525,948.77"
. When «decimal» is comma, then the dot character can be used to group digits, such as "525.948,77"
(for five hundred twenty-five thousand, nine hundred eight and 77 hundredths).
Quote character
To use a different character for quotes, specify the character in the «quote» parameter. The default is to use the double quote ("
). The most common other quote character used is the single quote ('). To type this for the parameter in Analytica, you can either type four single quotes ('
) or type doubleQuote SingleQuote doubleQuote ("'"
).
The CSV format
There is no single well-defined CSV standard. Undocumented conventions used by Excel are often seen as the most definitive "specification". The article at Comma Separated Values (CSV) Standard File Format is relatively easy to read and covers the basics well, without being the most comprehensive reference.
Each "record" (or row) is a CSV file is delineated by a newline, either CR, LF or CRLF. Each value within a row is separated from the next field by a separator character, usually comma (hence the name "Comma-separated values"), although other characters or character sequences are also sometimes used, including commonly TAB (Chr(9)
), bar ('|'
), semi-colon (';'
) and even space.
A row of data might look like this:
California, CA, "39,144,818", 163696, Sacramento, "Edmund Gerald ""Jerry"" Brown, Jr.", ="95814", 9/9/1850
Both textual and numeric values may or may not be surrounded by quotes. Any value containing the separator character or a newline must be surrounded by quotes. Quotes are always the double-quote character ("
). A value may also be a date (e.g., 9/9/1850
). Spaces to the left or right of a value are normally trimmed (unless you set «textTrim» to false).
When a value is read, if it parses as a valid date, it will be parsed as a date, otherwise if it parses as a valid number, it will parse as a number, and otherwise it will be read as text. The «parseFlags» parameter can be used to alter this behavior somewhat. To force a value to be read as text, even when it would be a valid number, use the equal-quote form, ="95814"
.
Note that new lines can appear within quoted values, so simply splitting CSV data on newlines does not reliably separate the data into rows.
The first line of a CSV file often consists of columns names, with data starting on the second row. But this is not always the case, some CSV files have no column headers with data starting on the first line.
Examples
The simplest usage is:
ParseCSV( csvText )
which follows the most common format and Excel standards.
To read in a CSV file:
ParseCSV( ReadTextFile( filename ) )
History
This built-in function was introduced in Analytica 5.0. For earlier releases, the Flat-file library contains ReadCsvFile and ParseCsvText functions. The ParseCSV is a substantial improvement.
See Also
- ReadTextFile
- MakeCSV
- SplitText, FindInText
- ParseNumber
- ParseDate
- ReadCsvFile, ParseCsvText -- these are made obsolete by ParseCSV, but are of use in Analytica 4.7 and earlier.
Enable comment auto-refresher