csv.lsp
Module: CSV
Author: Jeff Ober
Version: 1.1
Original location: http://static.artfulcode.net/newlisp/csv.lsp
Source: csv.lsp
Package definition: csv.qwerty
Functions for parsing CSV files (updated for newlisp 10)
Version history
1.1 • fixed incompatibilities with newlisp 10
1.0 • initial release- § -
CSV:make-row-parser
syntax: (CSV:make-row-parser str-delimiter str-quote-char)
parameter: str-delimiter - column delimiter
parameter: str-quote-char - character denoting quoted strings
Returns a lambda that is able to parse a single row of CSV text. The created function returns a list of column values.
example:(setq parser (CSV:make-row-parser "," "\"")) => parser function (parser "foo,bar,baz,bat") => ("foo" "bar" "baz" "bat") (setq parser (CSV:make-row-parser "|" "\"")) => parser function (parser "foo|bar|baz|bat") => ("foo" "bar" "baz" "bat")- § -
CSV:parse-string
syntax: (CSV:parse-string str-text [str-delimiter [str-quote-char]])
parameter: str-text - the text to be parsed
parameter: str-delimiter - column delimiter
parameter: str-quote-char - character denoting quoted strings
Parses a string of text as a CSV file. Returns a list with one element for each line; each element is a list of column values for each line in the text.
- § -
CSV:parse-file
syntax: (CSV:parse-file str-file [str-delimiter [str-quote-char]])
parameter: str-file - the file to be read and parsed
parameter: str-delimiter - column delimiter
parameter: str-quote-char - character denoting quoted strings
Parses a CSV text file. Returns a list with one element for each line; each element is a list of column values for each line in the text. parse-file parses line by line, rather than processing the entire string at once, and is therefore more efficient than parse-file.
Note: at least some versions of MS Excel use a single \r for line endings, rather than a line feed or both. newLISP's read-line will only treat \n or \r\n as line endings. If all columns are lumped into one flat list, this may be the culprit. In this case, use parse-string with read-file instead as the best alternative.
- § -
CSV:list->row
syntax: (CSV:list->row list-cols str-delimiter str-quote-char)
parameter: str-delimiter - column delimiter; defaults to ","
parameter: str-quote-char - character denoting quoted strings; defaults to "\""
parameter: str-quote-char - character denoting quoted strings
Generates one row of CSV data from the values in list-cols. Non-numeric elements are treated as quoted strings.
- § -
CSV:list->csv
syntax: (CSV:list->csv list-rows str-delimiter str-quote-char str-eol)
parameter: list-rows - list of row sets (each is a list of values)
parameter: str-delimiter - column delimiter; defaults to ","
parameter: str-quote-char - character denoting quoted strings; defaults to "\""
parameter: str-eol - end of line character; defaults to "\n"
Generates CSV string of a list of column value sets.
- ∂ -
Artful Code
generated with newLISP and newLISPdoc