
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

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- keithsheppard.name

Thanks for your reply. I have a working program now using Text.CSV module.
Do you see any that can be improved?
When I print, the screen reads "Just "abc"". How can I get rid of "Just" in
the most elegant way?
Thanks,
Hong
-- file: ch22/PodMain.hs
module Main where
import System.Environment (getArgs)
import Text.CSV
import qualified Data.Map as M
main = do
[args] <- getArgs
result <- parseCSVFromFile args
case result of
Left errmsg -> putStrLn "Error when parsing!"
Right contents -> map_header_records contents
map_header_records :: CSV -> IO ()
map_header_records [] = return ()
map_header_records (x:xs) = process x xs
process :: [String] -> CSV -> IO ()
process x [] = return ()
process x (y:ys) = do
let tuple = zip x y
let hash = M.fromList tuple
putStrLn (show (M.lookup "name" hash))
process x ys
On Wed, Sep 2, 2009 at 8:23 PM, Keith Sheppard
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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- keithsheppard.name

Hong Yang wrote:
Thanks for your reply. I have a working program now using Text.CSV module.
Do you see any that can be improved?
When I print, the screen reads "Just "abc"". How can I get rid of "Just" in the most elegant way?
Try Data.Maybe.fromMaybe: http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html#v:fro... Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/
participants (3)
-
Erik de Castro Lopo
-
Hong Yang
-
Keith Sheppard