Version: 4.2.0.2
3 CSV
The CSV language in Mirrors allows the programatic assembly of syntactically valid Comma Separated Values (CSV) data, most commonly used for interoperability with Microsoft Excel. Unlike the other languages in Mirrors, the CSV language is entirely procedure based and involves no custom syntax.
3.1 CSV language
(require (planet untyped/mirrors/csv/csv)) |
CSV is assembled in sheets or rows of cells. Cells are separated by commas and rows by newlines. String data in cells is appropriately escaped to ensure compatibility with Microsoft Excel.
(csv:sheet rows ...) → csv:sheet? |
rows : (U csv:row? (listof csv:row?)) |
Creates a sheet of CSV data from the specified rows. Each argument can be a row or a list of rows.
Examples: |
> (display | (csv->string | (csv:sheet (csv:row (csv:cell "Column 1") | (csv:cell "Column 2") | (csv:cell "Column 3")) | (for/list ([j (in-range 0 2)]) | (csv:row (for/list ([i (in-range 0 2)]) | (csv:cell (format "~a,~a" i j)))))))) |
|
"Column 1","Column 2","Column 3" | "0,0","1,0" | "0,1","1,1" |
|
(csv:row cells ...) → csv:row? |
cells : (U csv:cell? (listof csv:cell?)) |
Creates a row of CSV data from the specified cells. Each argument can be a cell or a list of cells. See above for examples.
(csv:cell datum) → csv:cell? |
datum : (U boolean? number? string? symbol? bytes? url? time-utc? time-tai?) |
Creates a CSV cell containing an appropriately escaped datum. See above for examples.
(csv:sheet? arg) → boolean? |
arg : any |
Predicate that identifies CSV sheet structures.
(csv:row? arg) → boolean? |
arg : any |
Predicate that identifies CSV row structures.
(csv:cell? arg) → boolean? |
arg : any |
Predicate that identifies CSV cell structures.
(csv? arg) → boolean? |
arg : any |
Predicate that identifies CSV sheets, rows or cells.
3.2 Rendering CSV data and sending CSV responses
(require (planet untyped/mirrors/csv/csv-response)) |
3.2.1 Rendering CSV data in string form
(csv->string val) → string |
val : csv |
Renders a CSV sheet as a string.
(make-csv-response | [ | #:code code | | | | | | | #:message message | | | | | | | #:seconds seconds | | | | | | | #:mime-type mime-type | | | | | | | #:headers headers] | | | | | | | content) | | → | | response |
|
code : integer = 200 |
message : string = "OK" |
seconds : integer = (current-seconds) |
mime-type : (U string bytes) = #"text/xml; charset=utf-8" |
headers : (alistof symbol string) = no-cache-http-headers |
content : csv |
Takes a csv content and wraps it in an HTTP response object that can be used with the PLT web server (including procedures such as send/suspend and send/suspend/dispatch). The keyword arguments correspond to the first five arguments of make-response/full.