
csv.pl -- Process CSV (Comma-Separated Values) dataThis library parses and generates CSV data. CSV data is represented in Prolog as a list of rows. Each row is a compound term, where all rows have the same name and arity.
csv_read_file(+File, -Rows) is det
csv_read_file(+File, -Rows, +Options) is det\t for
.tsv files and , otherwise.
Suppose we want to create a predicate table/6 from a CSV file
that we know contains 6 fields per record. This can be done
using the code below. Without the option arity(6), this would
generate a predicate table/N, where N is the number of fields
per record in the data.
?- csv_read_file(File, Rows, [functor(table), arity(6)]), maplist(assert, Rows).
csv_read_stream(+Stream, -Rows, +Options) is det
csv(?Rows)// is det
csv(?Rows, +Options)// is detseparator(0';) parses
a semicolon separated file.true (default false), threat double quotes as a normal
character.true (default false), strip leading and trailing
blank space. RFC4180 says that blank space is part of the
data.#. After
skipping comment lines this option causes csv//2 to skip empty
lines. Note that an empty line may not contain white space
characters (space or tab) as these may provide valid data.true (default), use name/2 on the field data. This
translates the field into a number if possible.down, downcase atomic values. If up, upcase them
and if preserve (default), do not change the case.row.domain_error(row_arity(Expected), Found) if a row is
found with different arity.false (default true), do not reject CSV files where
lines provide a varying number of fields (columns). This
can be a work-around to use some incorrect CSV files.
csv_read_file_row(+File, -Row, +Options) is nondetIn addition to the options of csv_read_file/3, this predicate processes the option:
csv_read_row(+Stream, -Row, +CompiledOptions) is detend_of_file upon reaching the
end of the input.
csv_options(-Compiled, +Options) is det
csv_write_file(+File, +Data) is det
csv_write_file(+File, +Data, +Options) is det\t for
.tsv files and , otherwise.
csv_write_stream(+Stream, +Data, +Options) is det
save_data(File) :-
setup_call_cleanup(
open(File, write, Out),
forall(data(C1,C2,C3),
csv_write_stream(Out, [row(C1,C2,C3)], [])),
close(Out)).