From b04d20a373459c759682d5182d1b92fed0208c62 Mon Sep 17 00:00:00 2001 From: Andrew Davison Date: Mon, 1 May 2023 19:11:24 +1000 Subject: [PATCH] Fix parse_csv_file/2 to add header/1 option to skip first line --- README.md | 1 + src/predicates.c | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dfd9153ee..fdb1929ff 100644 --- a/README.md +++ b/README.md @@ -643,6 +643,7 @@ Where options can be: trim(Boolean) # default false, trims leading and trailing whitespace numbers(Boolean) # default false, converts integers and floats + header(Boolean) # default false, skip first (header) line strings(Boolean) # default depends on type of input (atom ot string) assert(Boolean) # default false, assertz to database instead (assumed for files, needs a functor) functor(Atom) # default output is a list, create a structure (mandatory for files and with assert) diff --git a/src/predicates.c b/src/predicates.c index 07ddf1798..9e2193c4d 100644 --- a/src/predicates.c +++ b/src/predicates.c @@ -7489,7 +7489,8 @@ static bool fn_parse_csv_file_2(query *q) { GET_FIRST_ARG(p1,atom); GET_NEXT_ARG(p3,list_or_nil); - bool trim = false, numbers = false, use_strings = is_string(p1), do_assert = true; + bool trim = false, numbers = false, use_strings = is_string(p1); + bool header = false, do_assert = true; const char *functor = NULL; int sep = ',', quote = '"'; LIST_HANDLER(p3); @@ -7505,6 +7506,8 @@ static bool fn_parse_csv_file_2(query *q) trim = true; else if (!strcmp("numbers", C_STR(q, h)) && is_atom(c) && (c->val_off == g_true_s)) numbers = true; + else if (!strcmp("header", C_STR(q, h)) && is_atom(c) && (c->val_off == g_true_s)) + header = true; else if (!strcmp("strings", C_STR(q, h)) && is_atom(c) && (c->val_off == g_true_s)) use_strings = true; else if (!strcmp("strings", C_STR(q, h)) && is_atom(c) && (c->val_off == g_false_s)) @@ -7544,6 +7547,11 @@ static bool fn_parse_csv_file_2(query *q) line[len] = '\0'; + if (header) { + header = false; + continue; + } + if (!do_parse_csv_line(q, sep, quote, trim, numbers, use_strings, functor, line, NULL, 0)) fprintf(stderr, "Error: line %u\n", line_nbr+1);