- make-format #!optional DELIMITERprocedure
Returns procedures for outputting individual field values, CSV records, and lists of CSV records, where each list is printed on a separate line.
Procedure FORMAT-CELL takes in a value, obtains its string representation via format, and surrounds the string with quotes, if it contains characters that need to be escaped (such as quote characters, the delimiter character, or newlines).
Procedure FORMAT-RECORD takes in a row and returns its string representation, based on the strings produced by FORMAT-CELL and the delimiter character. A row can be given either as a bare list of field values, or as a csv-record (e.g. one obtained back from make-parser) -- there is no need to wrap a plain list with list->csv-record before printing it.
Procedure FORMAT-CSV takes in a list of rows (each a bare list or a csv-record; the two can be freely mixed) and produces a string representation using FORMAT-RECORD, with a trailing CRLF after every record.
Example:
(import csv-abnf) (define-values (fmt-cell fmt-record fmt-csv) (make-format #\;)) (fmt-cell "hello") => "hello" ;; This is quoted because it contains delimiter-characters (fmt-cell "one;two;three") => "\"one;two;three\"" ;; This is quoted because it contains quotes, which are then doubled for escaping (fmt-cell "say \"hi\"") => "\"say \"\"hi\"\"\"" ;; Rows are plain lists -- no list->csv-record wrapping needed (fmt-record '("hi there" "let's say \"hello world\" again" "until we are bored")) => "hi there;\"let's say \"\"hello world\"\" again\";until we are bored" ;; And an example of how to quickly convert a list of lists ;; to a CSV string containing the entire CSV file (fmt-csv '(("one" "two") ("and another \"line\"" "of csv stuff"))) => "one;two\r\n\"and another \"\"line\"\"\";of csv stuff\r\n" ;; csv-record objects (e.g. rows just read back with make-parser) work ;; directly too, with no unwrap/rewrap step: (fmt-csv (map list->csv-record '(("one" "two") ("and another \"line\"" "of csv stuff")))) => "one;two\r\n\"and another \"\"line\"\"\";of csv stuff\r\n"