csv library provides predicates for reading and writing CSV
files and streams:
The main object,
csv/3, is a parametric object allowing passing
options for the handling of the header of the file, the fields
separator, and the handling of double-quoted fields. The
csv/3 parametric object using default option values.
The library also include predicates to guess the separator and guess the number of columns in a given CSV file.
Files and streams can be read into a list of rows (with each row being represented by a list of fields) or asserted using a user-defined dynamic predicate.
Data can be saved to a CSV file or stream by providing the object and predicate for accessing the data plus the name of the destination file or the stream handle or alias.
To load all entities in this library, load the
| ?- logtalk_load(csv(loader)).
To test this library predicates, load the
| ?- logtalk_load(csv(tester)).
csv(Header, Separator, IgnoreQuotes) parametric object allows
passing the following options:
Header: possible values are
Separator: possible values are
IgnoreQuotes: possible values are
trueto ignore double quotes surrounding field data and
falseto preserve the double quotes.
csv object uses the default values
When writing CSV files or streams, set the quoted fields option to
false to write all non-numeric fields double-quoted (i.e. escaped).
The library objects can also be used to guess the separator used in a CSV file if necessary. For example:
| ?- csv::guess_separator('test_files/crlf_ending.csv', Separator). Is this the proper reading of a line of this file (y/n)? [aaa,bb,ccc] |> y. Separator = comma ?
This information can then be used to read the CSV file returning a list of rows:
| ?- csv(keep, comma, true)::read_file('test_files/crlf_ending.csv', Rows). Rows = [[aaa,bbb,ccc],[zzz,yyy,xxx]] ?
Alternatively, The CSV data can be saved using a public and dynamic object predicate. For example:
| ?- csv(keep, comma, true)::read_file('test_files/crlf_ending.csv', user, p/3). ** yes | ?- p(A,B,C). A = aaa B = bbb C = ccc ? ; A = zzz B = yyy C = xxx
Given a predicate representing a table, the predicate data can be written to a file or stream. For example:
| ?- csv(keep, comma, true)::write_file('output.csv', user, p/3). ** yes
leaving the content just as the original file thanks to the use of
true for the
| ?- csv(keep, comma, false)::write_file('output.csv', user, p/3). ** yes
results in the following file content:
guess_arity/2 method, to identify the arity, i. e. the number of
fields or columns per record in a given CSV file, for example:
| ?- csv(keep, comma, false)::guess_arity('test_files/crlf_ending.csv', Arity). Is this the proper reading of a line of this file (y/n)? [aaa,bbb,ccc] |> y. Arity = 3