CSV
This chapter describes the Bigloo API for processing CSV spreadsheets. This chapter has been written by Joseph Donaldson, as the implementation of the CSV library.Overview
The Bigloo csv library supports the parsing of csv and csv-like data. By default, it enables the parsing of comma, tab, and pipe separated data. In addition, facilities are provided that enable extending the library to support additonal csv-like formats. The rest of this document describes the Bigloo csv application programming interface.API Reference
read-csv-record input-port [custom-lexer]bigloo procedure
read-csv-record
has one required argument, the input-port of
the csv data to parse, and an optional argument indicating the lexer
to use, by default the lexer supporting standard csv files. It returns
a single record, as a list, or #eof-object
. Upon error, it will
throw an &invalid-port-error
or &io-parse-error
exception.
.keep
read-csv-records input-port [custom-lexer]bigloo procedure
read-csv-records
has one required argument, the input-port of
the csv data to parse, and an optional argument indicating the lexer
to use, by default the lexer supporting standard csv files. It returns
all of the records, as a list of lists, or #eof-object
. Upon
error, it will throw an &invalid-port-error
or
&io-parse-error exception
.
.keep
csv-for-each proc input-port [custom-lexer]bigloo procedure
csv-for-each
has two required arguments, a procedure to apply
to each record and the input-port of the csv data to parse, and an
optional argument indicating the lexer to use, by default the lexer
supporting standard csv files. It returns #unspecified
. Upon
error, it will throw an &invalid-port-error
or
&io-parse-error exception
.
.keep
csv-map proc input-port [custom-lexer]bigloo procedure
csv-map
has two required arguments, a procedure to apply to
each record and the input-port of the csv data to parse, and an
optional argument indicating the lexer to use, by default the lexer
supporting standard csv files. It returnsthe results of applying
proc
to each record as a list. Upon error, it will throw an
&invalid-port-error
or &io-parse-error
exception.
.keep
make-csv-lexer sep quotbigloo form
make-csv-lexer
has two required arguments, a character used to
separate records and a character for quoting. It returns custom lexer.
.keep
bigloo variable +csv-lexer+variable+csv-lexer+ is a bigloo-csv lexer supporting the standard comma-separated value format.
1
bigloo variable +tsv-lexer+variable+tsv-lexer+ is a bigloo-csv lexer supporting the tab-separated value format.
1
bigloo variable +psv-lexer+variable+psv-lexer+ is a bigloo-csv lexer supporting the pipe-separated value format.
1
The following is a simple example of using the bigloo-csv library. It parses a single record from the given csv data and prints it.
(module example (library bigloo-csv) (main main)) (define +csv-data+ "dog,cat,horse\npig,cow,squirrel") (define (main args) (let ((in (open-input-string +csv-data+))) (unwind-protect (print (read-csv-record in)) (close-input-port in))))