τau /

encoding/csv

module
csv = import("encoding/csv")

csv - comma separated values, the way RFC 4180 writes them.

Parse turns the text into a list of records, each one a list of fields, and Format writes that back out:

csv = import("encoding/csv")
os = import("os")
rows = csv.Parse(os.ReadFileString("cities.csv"), null)
println(rows[0])
os.WriteFile("out.csv", csv.Format(rows, null))

A field may be quoted with ", and a quote inside a quoted field is written twice. Quoted fields may hold the separator and newlines. Both \n and \r\n end a record. The separator is a single character, "," when it is null.

ponytail: the whole text at once, no reader over a stream. A file that doesn't fit in memory needs one, and that is when to write it.

Parse = fn(text, sep)source

Parse reads text and returns its records. A record is a list of strings, and every one of them is returned as it was written, without a guess at what it means. An unterminated quoted field is an error.

FormatField = fn(field, sep)source

FormatField returns one field written the way a CSV file wants it, quoted only when it has to be.

Format = fn(rows, sep)source

Format writes the records as CSV text, one line each, the last one ended by a newline as well.