質問

I try to parse a csv file and extractd a specific coloumn data from each row, I've managed to parse the file to a list of records, but I cannot iterate over the fields of each of the records. Is their a way to parse the csv file using prolog so that each record of the csv file will itself be a list?

My code:

:- use_module(library(csv)).

% main function ,  first parse the file then extracts max total market  
get_rows_data(File,Rows) :-
   csv_read_file(File, Rows, [functor(record), arity(18)]),
   maplist(assert, Rows),
   get_row(Rows).

get_row([]).    
get_row([Head|Tail]) :-
   write('*********************************'),write(Head),
   get_row(Tail). 
役に立ちましたか?

解決

I would say that you are almost there. You only need to convert the record term to a list.

:- use_module(library(apply)).
:- use_module(library(csv)).

get_rows_data(File, Lists):-
  csv_read_file(File, Rows, []),
  rows_to_lists(Rows, Lists).

rows_to_lists(Rows, Lists):-
  maplist(row_to_list, Rows, Lists).

row_to_list(Row, List):-
  Row =.. [row|List].
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top