[ library(csv) | Reference Manual | Alphabetic Index ]

csv_read(+File, -Rows, +Options)

Read a file containing comma separated values (csv format)
File
File name (string or atom)
Rows
List of lists or structures (output)
Options
List of options

Description

Reads a file containing comma separated values, and returns the file content as a list. The file may have an optional .csv suffix.

The result list contains one element for each record/row in the file. By default, each list element is itself a list, containing the row's field values. Alternatively, the type-option can be used to return structures instead of lists.

The data elements are strings, unless they can be interpreted as numbers (by ECLiPSe's number_string/2 predicate) and the 'convert' option is true (the default).

Options are:

convert:Bool (default true)
If a field can be converted to an integer or float using number_string/2, return this number. Otherwise return a string.
strip:Bool (default false)
Strip leading and trailing space from the field value. By default this is part of the data.
type:Type (default 'list')
Data type of returned rows: 'list' for a list of fields, name/arity for terms with this functor (arity can be left uninstantiated).

Modes and Determinism

Examples

    % Given file data.csv containing the line:
    % a, b, 123, c d," e f "

    ?- csv_read("data.csv", Rows, []).
    Rows = [["a", " b", 123, " c d", " e f "]]

    ?- csv_read("data.csv", Rows, [strip:true, convert:false]).
    Rows = [["a", "b", "123", "c d", " e f "]]

    ?- csv_read("data.csv", Rows, [strip:true, type:row/N]).
    Rows = [row("a", "b", 123, "c d", " e f ")]
    N = 5

See Also

number_string / 2, csv_read_row / 3