[Haskell-beginners] Processing individual csv line

Shakthi Kannan shakthimaan at gmail.com
Mon Jan 24 11:14:29 CET 2011


Hi,

I am trying to parse a .csv file and process the individual lines.
Here is some code snippet (based on RWH Chapter 16 [1]):

=== test.hs ===

import Text.ParserCombinators.Parsec
import System.Environment (getArgs)

parseCSV :: String -> Either ParseError [[String]]
parseCSV input = parse csvFile "(unknown)" input

main = do
    do c <- getContents
       case parse csvFile "(stdin)" c of
            Left e -> do putStrLn "Error parsing input:"
                         print e
	    Right r -> mapM_ print r

=== END ===

Given an input file foo.csv:

a,b,c
d,e,f
g,h,i

and running the above test.hs using:

  $ runghc test.hs < foo.csv

I get the following output:

["a,b,c"]
["d,e,f"]
["g,h,i"]

Instead of using mapM_ print r, how can I obtain and pass each of the
above lists (for example, ["a,b,c"]) to another function for
processing?

Appreciate any inputs,

Thanks!

SK

[1] RWH. Using Parsec. http://book.realworldhaskell.org/read/using-parsec.html

-- 
Shakthi Kannan
http://www.shakthimaan.com



More information about the Beginners mailing list