[manual index][section index]


csv - comma-separated values


include "bufio.m";
include "csv.m";
csv := load CSV CSV->PATH;

init:    fn(bufio: Bufio);
getline: fn(fd: ref Bufio->Iobuf): list of string;
quote:   fn(s: string): string;


CSV provides a few functions to help read and write data in ``comma-separated value'' format. The csv format has a sequence of fields on a line (terminated by a newline, carriage return, or carriage return and newline). Fields are separated by commas, and may be empty. A field that starts with a double-quote is a quoted field, continuing to the next lone double-quote character, including commas and line terminators, but taking two double-quotes to represent one double-quote. Any text following quoted text, up to the next comma or line terminator, is included in the field value. Note that in a field that does not start with a double-quote, double-quotes are not treated specially.

Init must be called before any other operation of the module. Bufio is the instance of the bufio(2) module that will provide the open files to be read.

Getline reads a sequence of fields in csv format from Iobuf fd, unquotes fields as required, and returns them in order in a list. It returns nil on end-of-file (or read error). An empty line is regarded as containing a single empty field.

Quote returns string s quoted as required by the csv format: the result is quoted if s contains comma, newline, or a quote (and all embedded quotes are doubled).





CSV(2 ) Rev:  Tue Mar 31 02:42:39 GMT 2015