[Haskell-beginners] Help with CSV

Keith Sheppard keithshep at gmail.com
Wed Sep 2 21:23:05 EDT 2009


Not quite code but...

here is an example of parsing CSV
http://book.realworldhaskell.org/read/using-parsec.html and here is a
library that you can use which is similar
http://hackage.haskell.org/package/csv

These approaches give you a 2D String list that you can do whatever
you want with.

if you need to turn a string into a double and you know the string is
well formed i think the syntax looks like

> let doubleVal = read stringVal :: Double

There are better ways to do this if you need to be able to handle
formatting errors but I don't know them off the top of my head

-Keith

On Wed, Sep 2, 2009 at 6:40 PM, Hong Yang<hyangfji at gmail.com> wrote:
> I need to process csv files that have the characteristics as follows:
> 1)    each file has thousands of columns which have String, Int, and Double
> types
> 2)    the number of columns may change
> 3)    for those columns whose name do not change, their location may change
>
> I want to process some columns in 3) using Haskell.
>
> In Perl, I can easily have the code like below:
>
> use Text::CSV;
> my $csv = Text::CSV->new( { allow_whitespace => 1 } );
> open my $temp, "<", "temp.csv" or die "Cannot open temp.csv! ($!)";
> my @fields = @{ $csv->getline($temp) };
> $csv->column_names(@fields);
> while ( my $hr = $csv->getline_hr($temp) ) {
>     my $sn = $hr->{"UNIT:unitSerialNumber"};
>     # processing goes here ...
> }
> close $temp;
>
> Can someone please give me an equivalent code in Haskell? Then I can digest
> and expand it.
>
> Thanks,
>
> Hong
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>



-- 
keithsheppard.name


More information about the Beginners mailing list