A.11 library(csv): Process CSV (Comma-Separated Values) data
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • The SWI-Prolog library
        • library(csv): Process CSV (Comma-Separated Values) data
          • csv_read_file/2
          • csv_read_file/3
          • csv_read_stream/3
          • csv//1
          • csv//2
          • csv_read_file_row/3
          • csv_read_row/3
          • csv_options/2
          • csv_write_file/2
          • csv_write_file/3
          • csv_write_stream/3
    • Packages
Availability::- use_module(library(csv)).(can be autoloaded)
Source[det]csv_read_file(+File, -Rows)
[det]csv_read_file(+File, -Rows, +Options)
Read a CSV file into a list of rows. Each row is a Prolog term with the same arity. Options is handed to csv//2. Remaining options are processed by phrase_from_file/3. The default separator depends on the file name extension and is \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).