Re: Skip rows (number of rows?), column type in library(csv)

Boris Vassilev <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <CAFw8osLaUSNRkK8_4t627k57ByYWz0o4S=5GQ9h1OKSxibYpLQ@mail.gmail.com>
Hi Wouter,

no, I did not consider creating a pack. These can be just additional
options to csv//2 <http://www.swi-prolog.org/pldoc/doc_for?object=csv/2> and
csv_read_file_row/3<http://www.swi-prolog.org/pldoc/doc_for?object=csv_read_file_row/3>
which
are entirely backward compatible (as far as I can see from the current
documentation and implementation). It seems that creating a pack is only
going to muddy the waters, by adding a layer on top of library(csv) for
something quite trivial and obvious.

Cheers,
Boris


On Tue, Sep 24, 2013 at 2:26 PM, Wouter Beek <[email protected]> wrote:

> Hi Boris,
>
> These options seem indeed quite useful for some use cases involving CSV
> data. I have no opinion as to whether or not they ought to be part of the
> core feature set. Did you consider creating a Pack for them?
> http://www.swi-prolog.org/pack/list
>
> ---
> Cheers,
> Wouter.
>
> E-mail: [email protected]
> WWW: www.wouterbeek.com
> Tel.: 0647674624
>
>
> On Tue, Sep 24, 2013 at 10:05 AM, Boris Vassilev <[email protected]
> > wrote:
>
>> Hello,
>>
>> I am using the (highly useful) library(csv) and I had to do two
>> tricks, in order to:
>> 1. Skip the header;
>> 2. Control (and validate) the datatype of the columns.
>>
>> It is easiest to explain with the code:
>>
>> load_csv.pl
>> ==
>> :- module(load_csv,
>>     [load_csv_file/4
>>     ]).
>>
>> :- use_module(library(csv)).
>>
>> load_csv_file(File, Functor, Skip, ColumnTypes) :-
>>     FirstLine is Skip + 1,
>>     forall(
>>         load_table_row(File, Functor, ColumnTypes, Line, Row),
>>         (   Line >= FirstLine
>>         ->  assertz(Row)
>>         ;   true
>>         )
>>     ).
>>
>> load_table_row(File, Functor, ColumnTypes, Line, Row) :-
>>     csv_read_file_row(
>>         File,
>>         R,
>>         [convert(false),line(Line)]
>>     ),
>>     R =.. [row|RawColumns],
>>     maplist(convert_field, ColumnTypes, RawColumns, Columns),
>>     Row =.. [Functor|Columns].
>>
>> convert_field(atom, Field, Field).
>> convert_field(number, Field, Number) :-
>>     atom_number(Field, Number).
>> % add additional types as needed
>> ==
>>
>> Is there a way to achieve this using the available options that I didn't
>> see?
>>
>> Does anyone thinks it would be useful to add options to the library for
>> A. Skipping N lines at the beginning;
>> B. Loading at most N lines;
>> C. Pass a list of datatypes for validation and convertion of the data
>> fields?
>>
>> In other words, adding options
>> A. 'skip(+Integer)' with a default 0;
>> B. 'nrows(+Integer)' with a default -1 meaning all (or maybe 'infinite'?)
>> C. 'col_types(+List:atom)' which could be for example 'atom',
>> 'number', 'integer', 'float', etc. There is a 'convert(+Boolean)'
>> option at the moment but it only gets in the way if the column has
>> values like "00", "01", ..., "A0", ..., "QZ", etc.
>>
>> If there is any interest and we agree on the interface I can submit a
>> proposed implementation.
>>
>> Cheers,
>> Boris
>> _______________________________________________
>> SWI-Prolog mailing list
>> [email protected]
>> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>>
>>
>
-------------- next part --------------
HTML attachment scrubbed and removed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.