title: csv parser in erlang
body: <p>for my application i have to parse CSV file using Erlang.following is the code which will parse CSV using Erlang:-</p>
  
  <pre><code>parse_file(Fn) -&gt;
  {ok, Data} = file:read_file(Fn),
  parse(binary_to_list(Data)).
  
  parse(Data) -&gt; lists:reverse(parse(Data, [])).
  
  parse([], Acc) -&gt; Acc;
  parse(Data, Acc) -&gt;
  {Line, Tail} = parse_line(Data),
  parse(Tail, [Line|Acc]).
  
  parse_line(Data) -&gt;
  {Line, Tail} = parse_line(Data, []),
  {lists:reverse(Line), Tail}.
  
  parse_line([13,10|Data], Acc) -&gt; {Acc, Data};
  parse_line([10|Data], Acc) -&gt; {Acc, Data};
  parse_line([13|Data], Acc) -&gt; {Acc, Data};
  parse_line([], Acc) -&gt; {Acc, []};
  parse_line([$,,$,|Data], Acc) -&gt; parse_line(Data, [""|Acc]);
  parse_line([$,|Data], Acc) -&gt; parse_line(Data, Acc);
  parse_line(Data, Acc) -&gt;
  {Fld, Tail} = parse_field(Data),
  parse_line(Tail, [Fld|Acc]).
  
  parse_field([34|Data]) -&gt;
  {Fld, Tail} = parse_fieldq(Data, ""),
  {lists:reverse(Fld), Tail};
  parse_field(Data) -&gt;
  {Fld, Tail} = parse_field(Data, ""),
  {lists:reverse(Fld), Tail}.
  
  parse_field([$,|Tail], Acc) -&gt; {Acc, [$,|Tail]};
  parse_field([13|Tail], Acc) -&gt; {Acc, [13|Tail]};
  parse_field([10|Tail], Acc) -&gt; {Acc, [10|Tail]};
  parse_field([], Acc) -&gt; {Acc, []};
  parse_field([Ch|Tail], Acc) -&gt; parse_field(Tail, [Ch|Acc]).
  
  parse_fieldq([34,34|Tail], Acc) -&gt; parse_fieldq(Tail, [34|Acc]);
  parse_fieldq([34|Tail], Acc) -&gt; {Acc, Tail};
  parse_fieldq([Ch|Tail], Acc) -&gt; parse_fieldq(Tail, [Ch|Acc]).
  </code></pre>
  
  <p>this code works fine but having two issues:-
  1-since the code parse using double quote ("") and comma(,) and separate each value..but in following example if First name consist of double quote sting within it then the parser will create one more field.</p>
  
  <pre><code>"Type","First Name","Last Name","Email"
  "Contact","Ashwani  Garg ------"All Pain Will End."","","itisashwani4u@gmail.com"
  
  result:-
  [["contact"],["Ashwani  Garg ------"],["All Pain Will End."],[],["itisashwani4u@gmail.com"]]
  
  expected result:-
  [["contact"],["Ashwani  Garg ------All Pain Will End."],[],["itisashwani4u@gmail.com"]]
  </code></pre>
  
  <p>2-for the following kind of csv its for value,its truncate some value:-
  First Name,Last Name,Middle Name,Name,Nickname,E-mail Address,Home Street,Home City,Home Postal Code,Home State,Home Country/Region,Home Phone,Home Fax,Mobile Phone,Personal Web Page,Business Street,Business City,Business Postal Code,Business State,Business Country/Region,Business Web Page,Business Phone,Business Fax,Pager,Company,Job Title,Department,Office Location,Notes</p>
  
  <pre><code>    Affection,,,Affection,,,,,,,,+919845141544,,+919845141544,,,,,,,,,,,,,,,
      result:-
      [["Affection"],[],[],["Affection"],[],[],[],[],[],[],[],["+919845141544"],[],["+919845141544"],[],[],[],[],[],[],[]]
      expected result:-
     [["Affection"],[],[],["Affection"],[],[],[],[],[],[],[],["+919845141544"],[],["+919845141544"],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
  </code></pre>
  
  <p>Please help me ...for refernce please use the following link:-
    <a href="http://ppolv.wordpress.com/2008/02/25/parsing-csv-in-erlang/" rel="noreferrer">http://ppolv.wordpress.com/2008/02/25/parsing-csv-in-erlang/</a></p>