| Author: | Eddie Rucker <erucker@bmc.edu> |
|---|---|
| Date: | 2009-11-09 |
| Copyright: | 2008, 2009 by Eddie Rucker |
| License: | 3-clause BSD license, see the accompanying COPYING file for details |
The CSV library provides an interface for reading and writing comma separated value files that is loosely based on Python's CSV module (http://docs.python.org/lib/module-csv.html).
Contents
Run make to compile the module and make install (as root) to install it in the Pure library directory. This requires GNU make. The make install step is only necessary for system-wide installation.
The make utility tries to guess your Pure installation directory and platform-specific setup. If it gets this wrong, you can set some variables manually. In particular, make install prefix=/usr sets the installation prefix, and make PIC=-fPIC or some similar flag might be needed for compilation on 64 bit systems. Please see the Makefile for details.
Data records are represented as lists of strings, integers, and doubles. Three predefined dialects are provided; RFC4180 (default for Windows), UNIX (default for Linux, *BSD, and Unix), and EXCEL; and procedures are provided to create other CSV dialects. See (http://www.ietf.org/rfc/rfc4180.txt) for more details about the RFC4180 standard.
Examples of reading and writing CSV files are provided in the file "samples.pure" located in the examples subdirectory.
Examples
> using system, csv; > csv::str [1%4, 1%3]; // rational numbers are not allowed csv::error "field 1: invalid conversion type." > /* Now, print a message and > perform an exit action on the error */ > csv::error msg = printf "CSV Error: %s\n" msg $$ exit 0; > csv::str [1%4, 1%3]; CSV Error: field 1: invalid conversion type. $
creates a dialect from a list of dialect option pairs. Option pairs are denoted a=>b where a is the option and b is the option's value. The list of possible options and option values are presented below.
- Value - boolean {1=true, 0=false}.
- Default - 1.
- Note - Quoted fields always retain white space.
The following example illustrates the construction of a dialect for reading tab delimited files without quoted strings.
Examples
> using csv;
> /* Use the predefined EXCEL dialect */
> csv::list ("\"=\"\"004\",23\r\n", csv::EXCEL);
["=\"004",23]
> const d = csv::dialect [
> csv::DELIMITER => "\t",
> csv::QUOTING_STYLE => csv::QUOTE_EMBEDDED];
> csv::list ("\"John\tSmith\"\t23\t4.5\tfreshman", d);
["John\tSmith",23,4.5,"freshman"]
>
Examples
> using csv; > csv::str [1,"",3.00,"bob \"plays drums\""]; "1,\"\",3,\"bob \"\"plays drums\"\"\"\n" > const d = csv::dialect [csv::DELIMITER => "\t"]; > csv::str ([1,"",3.00,"bob \"plays drums\""], d); "1\t\"\"\t3\t\"bob \"\"plays drums\"\"\"\n" >
Examples
> using csv;
> csv::list "1,\"\",3,\"bob \"\"plays drums\"\"\"\n";
[1,"",3,"bob \"plays drums\""]
> const d = csv::dialect [csv::DELIMITER => "\t"];
> csv::list ("1\t\"\"\t3\t\"bob \"\"plays drums\"\"\"\n", d);
[1,"",3,"bob \"plays drums\""]
>
Examples
> using system, csv; > /* Write a file to test */ > let f = fopen "test.csv" "w"; > csv::fput [[1,"a"], [2,"b"], [3,"c"]] f; () > fclose f; 0 > let f = fopen "test.csv" "r"; > csv::fgets f; [1,"a"] > fclose f; 0 >
When modifying CSV files that will be imported into Microsoft Excel, fields with significant leading 0s should be written using a "=""0...""" formatting scheme. This same technique will work for preserving leading space too. Again, this quirk should only be necessary for files to be imported into MS Excel.
Examples
> using system, csv; > /* Write a file to test */ > let f = fopen "test.csv" "w"; > csv::fput [[1,"a"], [2,"b"], [3,"c"]] f; () > fclose f; 0 > let f = fopen "test.csv" "r"; > /* Read the first record */ > csv::fgets f; [1,"a"] > /* Read the next record */ > csv::fgets f; [2,"b"] > fclose f; 0 >