[erlang-questions] CSV file help

109 views
Skip to first unread message

Lucky Khoza

unread,
Nov 12, 2012, 2:54:25 AM11/12/12
to erlang-q...@erlang.org
Hi Erlang Developers,

Where do i start in terms of reading CSV file, because i tried to use this rountines:

{ok,String} = file:open("produce.csv",read).
io:get_line(String, ' ').

but when i used this rountine: io:get_line(String,' ') to read each line i got this tuple: {error,terminated},

And the file i'm trying to read is like this:

"Supplier ID","Product Code","Product Description","Delivery Date","Unit Price","Number of Units"
15,1101,"Apples 1kg Golden Delicious. The sweetest Apples! Always a favourite. Love, Mrs. Hollingberry","2012/02/15",1505,5

Thanks in advance for your help.

Kindest Regards
Lucky


Bengt Kleberg

unread,
Nov 12, 2012, 3:04:52 AM11/12/12
to Lucky Khoza, erlang-q...@erlang.org
Greetings,

The documentation page for io:file/2 says that the second argument
should be a list of atoms. Like this: [read].
If {ok, String} matches, then you are probably just lucky.


bengt
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Michael Uvarov

unread,
Nov 12, 2012, 7:20:16 AM11/12/12
to bengt....@ericsson.com, erlang-q...@erlang.org
Hi,

There are few libraries for CSV parsing.
I wrote this one. https://github.com/arcusfelis/csv_parser

--
Best regards,
Uvarov Michael

Jesper Louis Andersen

unread,
Nov 12, 2012, 7:30:40 AM11/12/12
to Lucky Khoza, erlang-q...@erlang.org

On Nov 12, 2012, at 8:54 AM, Lucky Khoza <mrk...@gmail.com> wrote:

> Hi Erlang Developers,
>
> Where do i start in terms of reading CSV file, because i tried to use this rountines:
>
> {ok,String} = file:open("produce.csv",read).
> io:get_line(String, ' ').

There are two problems here:

first, file:open/2 should be called appropriately and Bengt writes. Second, the binding {ok, String} should rather be {ok, IoDevice} in order to be more precise. The IO device needs to be used to read out data from the file in question. Look at the call file:read_line/2.

The io:get_line/2 call is probably ok, but I would prefer file:read_line/2 since it does not have to be given a prompt. Speaking of prompt, the value '' is not a string but an (empty) atom. In Erlang, you can't interchange ' and " like many other languages (almost) allows you to.

The {error, terminated} is probably because your IoDevice was not opened correctly and thus the call fails.

Jesper Louis Andersen
Erlang Solutions Ltd., Copenhagen
Reply all
Reply to author
Forward
0 new messages