Simple CSV parser (Excel-style, text/csv)
Damien Diederen <dd@crosstwine.com>
Package csv (global prefix VedaCsv) provides a simple parser matching the syntax produced by Microsoft Excel in an U.S. locale.
Basic usage:
$ cat Book1.csv A,1 B,2 "crazy""column with CR and all!",3
$ $CDSHOME/tools/dfII/bin/skill (load "pkg/csv/csv.ils") |- t (VedaCsvParseFile "./Book1.csv") |- (("A" "1") | ("B" "2") | ("crazy\"column\nwith CR and all!" "3"))
(defun ParseFile (filename) ...)
ParseFile parses filename as a CSV stream, returning a list of lists of strings; the upper-left corner of a spreadsheet filled with cell addresses would parse as:
(("A1" "B1" "C1") ("A2" "B2" "C2") ("A3" "B3" "C3"))
Escape sequences and in-cell carriage returns are supported, but no data interpretation is done besides parsing to strings.
Errors are thrown if filename cannot be open for reading, or when EOF is encountered within a quoted cell.
"Staggered" spreadsheets, or streams ending without a carriage return, are not error conditions; they produce the "obvious" result:
a,b,c\n (("a" "b" "c") d,e\n => ("d" "e") f,g,h\n ("f" "g" "h") i ("i"))
(defun ParseLines (lines) ...)
ParseLines parses a list of string "lines" as a CSV stream. The list of strings is interpreted as if joigned by:
(buildString lines "\n")
but the code doesn't do that to avoid triggering string length limits. Note that each "line" can also embed one or more \n characters.
Cf. ParseFile for more information and return specification.
(defun ParsePort (port) ...)
ParsePort parses text read from port as a CSV stream. Parsing finishes when EOF is reached, but the port is not closed.
Cf. ParseFile for more information and return specification.