Prolog,使用不同的分隔符从CSV读取数据

时间:2016-07-05 14:44:10

标签: csv prolog swi-prolog

我需要在prolog中读取不同的CSV文件,某些行使用0'\t格式化,而在其他文件中使用空格0'格式化。

我用过:

    read_points(Filename, Points) :-
       csv_read_file(Filename, P,[convert(true),functor(pt),separator(0'\t)]),
       csv_read_file(Filename, P,[convert(true),functor(pt),separator(0' )]).

但它没有用,因为给我两个不同的清单。

我怎样才能正确编码? 谢谢。

编辑: '0\t的示例文件:

0.1     5
3       5
5       8

'0的例子:

0.1 5
3 5
5 8

1 个答案:

答案 0 :(得分:0)

我使用If语句解析它并在第一行搜索是否有空格。

read(Filename, Elements) :-
(   space(Filename)
->   csv_read_file(Filename, L,[functor(line),separator(0' )])
;   csv_read_file(Filename, L,[functor(line),separator(0'\t)])
).

space(File):-
  read_first(File,L),
  once(member(32,L)).

read_first(File, sol) :-
  see(File),
  read_one_line(Codes),
  seen,
  Sol = Codes.

read_one_line(Codes) :-
  get0(Code),
  (   Code < 0 /* end of file */ ->
      Codes = []
  ;   Code =:= 10 /* end of line */ ->
      Codes = []
  ;   Codes = [Code|Codes1],
      read_one_line(Codes1)
  ).
相关问题