Top Documents Download Gallery Repository
Are you looking for a Software Engineer? Check Here

14csv Module

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.1Module Function

csv.parse
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:

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
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:

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.2csv.writer Class

14.2.1Constructor

csv.writer
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.2.2Method

csv.writer#write
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.3Extension of stream Class

This module extends the stream class with methods described here.

stream#read@csv
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:

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
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.


Copyright (C) 2011-2017 ypsitau