このファイルcsv.erlを作成します。
%%% --- csv parser in Erlang. ------
%%% To help process large csv files without loading them into
%%% memory. Similar to the xml parsing technique of SAX
-module(csv).
-compile(export_all).
parse(FilePath,ForEachLine,Opaque)->
case file:open(FilePath,[read]) of
{_,S} ->
start_parsing(S,ForEachLine,Opaque);
Error -> Error
end.
start_parsing(S,ForEachLine,Opaque)->
Line = io:get_line(S,''),
case Line of
eof -> {ok,Opaque};
"\n" -> start_parsing(S,ForEachLine,Opaque);
"\r\n" -> start_parsing(S,ForEachLine,Opaque);
_ ->
NewOpaque = ForEachLine(scanner(clean(clean(Line,10),13)),Opaque),
start_parsing(S,ForEachLine,NewOpaque)
end.
scan(InitString,Char,[Head|Buffer]) when Head == Char ->
{lists:reverse(InitString),Buffer};
scan(InitString,Char,[Head|Buffer]) when Head =/= Char ->
scan([Head|InitString],Char,Buffer);
scan(X,_,Buffer) when Buffer == [] -> {done,lists:reverse(X)}.
scanner(Text)-> lists:reverse(traverse_text(Text,[])).
traverse_text(Text,Buff)->
case scan("",$,,Text) of
{done,SomeText}-> [SomeText|Buff];
{Value,Rem}-> traverse_text(Rem,[Value|Buff])
end.
clean(Text,Char)->
string:strip(string:strip(Text,right,Char),left,Char).
csv ファイルからデータを抽出する関数を作成します。
test()->
ForEachLine = fun(Line,Buffer)-> io:format("Line: ~p~n",[Line]),Buffer end,
InitialBuffer = [],
csv:parse("/home/include/person.csv",ForEachLine,InitialBuffer).
erlang のコンソールでは、正しい結果が表示されます。
1> operation:test().
Line: ["50;97919861;5ab190537df;9898abb6e;efb65a0ad1;1",
"35427;1",
"3542;30-11-2012;testU2;testU6;undefined;m.;enabled;jjj;A999997;97979797;CIN;testU@gmail.com;b1088b51297846e;0;user;0;{{2012",
"11","30}","{1","59",
"26}};gggg;kkkk;ssss;standard;{2012","11",
"30};hhhh;mmm;undefined;18;yyyy;nnnn;false;{{2012",
"11","30}","{1","59","26}};1989;1;1"]
ok
今、私の目標は、このデータを mnesia データベースに登録することです (テーブル person 内)。
テーブルの人物には 49 の属性があります
この行を登録します
Line: ["50;97919861;5ab190537df;9898abb6e;efb65a0ad1;1",
"35427;1",
"3542;30-11-2012;testU2;testU6;undefined;m.;enabled;jjj;A999997;97979797;CIN;testU@gmail.com;b1088b51297846e;0;user;0;{{2012",
"11","30}","{1","59",
"26}};gggg;kkkk;ssss;standard;{2012","11",
"30};hhhh;mmm;undefined;18;yyyy;nnnn;false;{{2012",
"11","30}","{1","59","26}};1989;1;1"]
この構造を持つ表 person で:
-record(person, {id, token, password, pin, key, salt, pin_salt, subscription_date, first_name, last_name, alias, gender, status,
taxid, formid, idcard, id_type, email, activation_code, email_confirmation, role,
trying, last_access, last_activity, last_activity_ip, last_activity_op_code, group, last_mod, last_login,
agent_code, agency_code, parent_id,pass_changes, pin_changes, is_merchant, created_at, birth_year, birth_month, birth_date}).