14csv Module

14.1Overview

The csv module provices measures to read/write CSV files. To utilize it, import the csv module using import() function.

Below is an example to read a CSV file that contains three fields per line:

import(csv)

Record = struct(name:string, age:number, email:string)
records = Record * csv.read('records.csv')
printf('name:%s, age:%d, email:%s¥n',
       records:*name, records:*age, records:*email)

14.2Module Function

The csv module provides following functions:

csv.parse(str:string):map {block?}

Creates an iterator that parses a text in CSV format that is contained in the specified string and returns a list of fields as its each element.

In default, this returns an iterator as its result value. Specifying the following attributes would customize the returned value:

  • :iter .. An iterator. This is the default behavior.
  • :xiter .. An iterator that eliminates nil from its elements.
  • :list .. A list.
  • :xlist .. A list that eliminates nil from its elements.
  • :set .. A list that eliminates duplicated values from its elements.
  • :xset .. A list that eliminates duplicated values and nil from its elements.

See the chapter of Mapping Process in Gura Language Manual for the detail.

If a block is specified, it would be evaluated repeatingly with block parameters |value, idx:number| where value is the iterated value and idx the loop index starting from zero. In this case, the last evaluated value of the block would be the result value. If one of the attributes listed above is specified, an iterator or a list of the evaluated value would be returned.

csv.read(stream:stream:r) {block?}

Creates an iterator that parses a text in CSV format from the specified stream and returns a list of fields as its each element.

In default, this returns an iterator as its result value. Specifying the following attributes would customize the returned value:

  • :iter .. An iterator. This is the default behavior.
  • :xiter .. An iterator that eliminates nil from its elements.
  • :list .. A list.
  • :xlist .. A list that eliminates nil from its elements.
  • :set .. A list that eliminates duplicated values from its elements.
  • :xset .. A list that eliminates duplicated values and nil from its elements.

See the chapter of Mapping Process in Gura Language Manual for the detail.

If a block is specified, it would be evaluated repeatingly with block parameters |value, idx:number| where value is the iterated value and idx the loop index starting from zero. In this case, the last evaluated value of the block would be the result value. If one of the attributes listed above is specified, an iterator or a list of the evaluated value would be returned.

14.3csv.writer Class

14.3.1Constructor

csv.writer(stream:stream:w, format?:string) {block?}

Creates a csv.writer instance that provides methods to write CSV text to the specified stream.

The argument format specifies a printf-style format string that is used to convert a number and complex value to a string.

14.3.2Method

The csv.writer class provides following methods:

csv.writer#write(fields+):map:reduce

Writes values in CSV format.

The argument fields takes string, number or complex values that are to be put out in a row.

14.4Extension of stream Class

This module extends the stream class with methods described here.

stream#read@csv() {block?}

Creates an iterator that parses a text in CSV format from the specified stream and returns a list of fields as its each element.

In default, this returns an iterator as its result value. Specifying the following attributes would customize the returned value:

  • :iter .. An iterator. This is the default behavior.
  • :xiter .. An iterator that eliminates nil from its elements.
  • :list .. A list.
  • :xlist .. A list that eliminates nil from its elements.
  • :set .. A list that eliminates duplicated values from its elements.
  • :xset .. A list that eliminates duplicated values and nil from its elements.

See the chapter of Mapping Process in Gura Language Manual for the detail.

If a block is specified, it would be evaluated repeatingly with block parameters |value, idx:number| where value is the iterated value and idx the loop index starting from zero. In this case, the last evaluated value of the block would be the result value. If one of the attributes listed above is specified, an iterator or a list of the evaluated value would be returned.

stream#writer@csv(format?:string) {block?}

Creates a csv.writer instance that provides methods to write CSV text to the target stream.

The argument format specifies a printf-style format string that is used to convert a number and complex value to a string.