#lang scribble/doc @; THIS FILE IS GENERATED @(require scribble/manual) @(require (for-label (planet neil/csv:1:3))) @title[#:version "0.7"]{csv: Comma-Separated Value (CSV) Utilities in Scheme} @author{Neil Van Dyke} @defmodule[(planet neil/csv:1:3)] Official home page: @link["http://www.neilvandyke.org/csv-scheme/" #:underline? #f]{http://www.neilvandyke.org/csv-scheme/} @section{Introduction} The @bold{csv} Scheme library provides utilities for reading various kinds of what are commonly known as ``comma-separated value'' (CSV) files. Since there is no standard CSV format, this library permits CSV readers to be constructed from a specification of the peculiarities of a given variant. A default reader handles the majority of formats. One of the main uses of this library is to import data from old crusty legacy applications into Scheme for data conversion and other processing. To that end, this library includes various conveniences for iterating over parsed CSV rows, and for converting CSV input to the @link["http://pobox.com/~oleg/ftp/Scheme/SXML.html"]{SXML 3.0} Scheme XML format. This library requires R5RS, SRFI-6, SRFI-23, and an @tt{integer->char} procedure that accepts ASCII values. Other implementations of some kind of CSV reading for Scheme include Gauche's @tt{text.csv} module, and Scsh's @tt{record-reader} and related procedures. This library intends to be portable and more comprehensive. @section{Reader Specs} CSV readers are constructed using @italic{reader specs}, which are sets of attribute-value pairs, represented in Scheme as association lists keyed on symbols. Each attribute has a default value if not specified otherwise. The attributes are: @itemize[ @item{@tt{newline-type} Symbol representing the newline, or record-terminator, convention. The convention can be a fixed character sequence (@tt{lf}, @tt{crlf}, or @tt{cr}, corresponding to combinations of line-feed and carriage-return), any string of one or more line-feed and carriage-return characters (@tt{lax}), or adaptive (@tt{adapt}). @tt{adapt} attempts to detect the newline convention at the start of the input and assume that convention for the remainder of the input. Default: @tt{lax} } @item{@tt{separator-chars} Non-null list of characters that serve as field separators. Normally, this will be a list of one character. Default: @tt{(#\,)} (list of the comma character) } @item{@tt{quote-char} Character that should be treated as the quoted field delimiter character, or @tt{#f} if fields cannot be quoted. Note that there can be only one quote character. Default: @tt{#\"} (double-quote) } @item{@tt{quote-doubling-escapes?} Boolean for whether or not a sequence of two @tt{quote-char} quote characters within a quoted field constitute an escape sequence for including a single @tt{quote-char} within the string. Default: @tt{#t} } @item{@tt{comment-chars} List of characters, possibly null, which comment out the entire line of input when they appear as the first character in a line. Default: @tt{()} (null list) } @item{@tt{whitespace-chars} List of characters, possibly null, that are considered @italic{whitespace} constituents for purposes of the @tt{strip-leading-whitespace?} and @tt{strip-trailing-whitespace?} attributes described below. Default: @tt{(#\space)} (list of the space character) } @item{@tt{strip-leading-whitespace?} Boolean for whether or not leading whitespace in fields should be stripped. Note that whitespace within a quoted field is never stripped. Default: @tt{#f} } @item{@tt{strip-trailing-whitespace?} Boolean for whether or not trailing whitespace in fields should be stripped. Note that whitespace within a quoted field is never stripped. Default: @tt{#f} } @item{@tt{newlines-in-quotes?} Boolean for whether or not newline sequences are permitted within quoted fields. If true, then the newline characters are included as part of the field value; if false, then the newline sequence is treated as a premature record termination. Default: @tt{#t} } ] @section{Making Reader Makers} CSV readers are procedures that are constructed dynamically to close over a particular CSV input and yield a parsed row value each time the procedure is applied. For efficiency reasons, the reader procedures are themselves constructed by another procedure, @tt{make-csv-reader-maker}, for particular CSV reader specs. @defproc[(make-csv-reader-maker (reader-spec any/c)) any/c]{ Constructs a CSV reader constructor procedure from the @schemevarfont{reader-spec}, with unspecified attributes having their default values. For example, given the input file @tt{fruits.csv} with the content: @verbatim["apples | 2 | 0.42\nbananas | 20 | 13.69"] a reader for the file's apparent format can be constructed like: @SCHEMEBLOCK[ (define make-food-csv-reader (make-csv-reader-maker '((separator-chars . (#\|)) (strip-leading-whitespace? . #t) (strip-trailing-whitespace? . #t))))] The resulting @tt{make-food-csv-reader} procedure accepts one argument, which is either an input port from which to read, or a string from which to read. Our example input file then can be be read by opening an input port on a file and using our new procedure to construct a reader on it: @SCHEMEBLOCK[ (define next-row (make-food-csv-reader (open-input-file "fruits.csv")))] This reader, @tt{next-row}, can then be called repeatedly to yield a parsed representation of each subsequent row. The parsed format is a list of strings, one string for each column. The null list is yielded to indicate that all rows have already been yielded. @SCHEMEBLOCK[ (next-row) ==> ("apples" "2" "0.42") (next-row) ==> ("bananas" "20" "13.69") (next-row) ==> ()] } @section{Making Readers} In addition to being constructed from the result of @tt{make-csv-reader-maker}, CSV readers can also be constructed using @tt{make-csv-reader}. @defproc[(make-csv-reader (in any/c) (reader-spec any/c)) any/c]{ Construct a CSV reader on the input @schemevarfont{in}, which is an input port or a string. If @schemevarfont{reader-spec} is given, and is not the null list, then a ``one-shot'' reader constructor is constructed with that spec and used. If @schemevarfont{reader-spec} is not given, or is the null list, then the default CSV reader constructor is used. For example, the reader from the @tt{make-csv-reader-maker} example could alternatively have been constructed like: @SCHEMEBLOCK[ (define next-row (make-csv-reader (open-input-file "fruits.csv") '((separator-chars . (#\|)) (strip-leading-whitespace? . #t) (strip-trailing-whitespace? . #t))))] } @section{Basic Input Conveniences} Several convenience procedures are provided for iterating over the CSV rows and for converting the CSV into a list. To the dismay of some Scheme purists, each of these procedures accepts a @schemevarfont{reader-or-in} argument, which can be a CSV reader, an input port, or a string. If not a CSV reader, then the default reader constructor is used. For example, all three of the following are equivalent: @SCHEMEBLOCK[ (csv->list STRING ) == (csv->list (make-csv-reader STRING )) == (csv->list (make-csv-reader (open-input-string STRING )))] @defproc[(csv-for-each (proc any/c) (reader-or-in any/c)) any/c]{ Similar to Scheme's @tt{for-each}, applies @schemevarfont{proc}, a procedure of one argument, to each parsed CSV row in series. @schemevarfont{reader-or-in} is the CSV reader, input port, or string. The return } @defproc[(csv-map (proc any/c) (reader-or-in any/c)) any/c]{ Similar to Scheme's @tt{map}, applies @schemevarfont{proc}, a procedure of one argument, to each parsed CSV row in series, and yields a list of the values of each application of @schemevarfont{proc}, in order. @schemevarfont{reader-or-in} is the CSV reader, input port, or string. } @defproc[(csv->list (reader-or-in any/c)) any/c]{ Yields a list of CSV row lists from input @schemevarfont{reader-or-in}, which is a CSV reader, input port, or string. } @section{Converting CSV to SXML} The @tt{csv->sxml} procedure can be used to convert CSV to SXML format, for processing with various XML tools. @defproc[(csv->sxml (reader-or-in any/c) (row-element any/c) (col-elements any/c)) any/c]{ Reads CSV from input @schemevarfont{reader-or-in} (which is a CSV reader, input port, or string), and yields an SXML representation. If given, @schemevarfont{row-element} is a symbol for the XML row element. If @schemevarfont{row-element} is not given, the default is the symbol @tt{row}. If given @schemevarfont{col-elements} is a list of symbols for the XML column elements. If not given, or there are more columns in a row than given symbols, column element symbols are of the format @tt{col-@schemevarfont{n}}, where @schemevarfont{n} is the column number (the first column being number 0, not 1). For example, given a CSV-format file @tt{friends.csv} that has the contents: @verbatim["Binoche,Ste. Brune,33-1-2-3\nPosey,Main St.,555-5309\nRyder,Cellblock 9,"] with elements not given, the result is: @SCHEMEBLOCK[ (csv->sxml (open-input-file "friends.csv")) ==> (*TOP* (row (col-0 "Binoche") (col-1 "Ste. Brune") (col-2 "33-1-2-3")) (row (col-0 "Posey") (col-1 "Main St.") (col-2 "555-5309")) (row (col-0 "Ryder") (col-1 "Cellblock 9") (col-2 "")))] With elements given, the result is like: @SCHEMEBLOCK[ (csv->sxml (open-input-file "friends.csv") 'friend '(name address phone)) ==> (*TOP* (friend (name "Binoche") (address "Ste. Brune") (phone "33-1-2-3")) (friend (name "Posey") (address "Main St.") (phone "555-5309")) (friend (name "Ryder") (address "Cellblock 9") (phone "")))] } @section{History} @itemize[ @item{Version 0.7 -- 2009-02-22 -- PLaneT @tt{(1 3)} License is now LGPL 3. Moved to author's new Scheme administration system. } @item{Version 0.6 -- 2008-08-12 -- PLaneT @tt{(1 2)} For PLT 4 compatibility, new versions of @tt{csv-map} and @tt{csv->list} that don't use @tt{set-cdr!} (courtesy of Doug Orleans). PLT 4 @tt{if} compatibility change. Minor documentation fixes. } @item{Version 0.5 --- 2005-12-09 Changed a non-R5RS use of @tt{letrec} to @tt{let*}, caught by Guile and David Pirotte. } @item{Version 0.4 --- 2005-06-07 Converted to Testeez. Minor documentation changes. } @item{Version 0.3 --- 2004-07-21 Minor documentation changes. Test suite now disabled by default. } @item{Version 0.2 --- 2004-06-01 Fixed strange @tt{case}-related bug exhibited under Gauche 0.8 and 0.7.4.2 in @tt{csv-internal:make-portreader/positional}. Thanks to Grzegorz Chrupa/la for reporting. } @item{Version 0.1 --- 2004-05-31 First release, for testing with real-world input. } ] @section{Legal} Copyright (c) 2004--2009 Neil Van Dyke. This program is Free Software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (LGPL) as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See http://www.gnu.org/licenses/ for details. For other licenses and consulting, contact the author.