Can anybody recommend a way to parse CSV files with options to:
This is an old thread, but both csv-conduit and cassava have most, if not all -- not sure about re-writing to the file -- of the features you're looking for.
Cassava works in memory and is very simple library e.g.
encode [("John" :: Text, 27), ("Jane", 28)]
"John,27\r\nJane,28\r\n"
There is the Data.Csv module on hackage. In case your distribution does not provide a package for it you can install it via cabal, e.g.
$ cabal install cassava
It can read and write (i.e. decode/encode) records from/to CSV files.
You can set the field separator like this:
import Data.Csv
import Data.Char -- ord
import qualified Data.ByteString.Lazy.Char8 as B
enc_opts = defaultEncodeOptions {
encDelimiter = fromIntegral $ ord '\t'
}
write_csv vector = do
B.putStr $ encodeWith enc_opts vector
Currently, Data.Csv
does not offer other encode/decode options. There are function variants for working with a header row. As is, lines are terminated with CRLF, double-quotes are used for quoting and as text-encoding UTF8 is assumed. Double-quotes in values are quoted with a back-slash and quoting is omitted where it is 'not necessary'.
A quick search on Hackage finds Data.Spreadsheet, which does have customizable quote and separator.
I can't recommend a ready-to-go, packaged-up CSV parser for Haskell, but I remember that the book Real-World Haskell by Bryan O'Sullivan et al. contains a chapter on Parsec, which the authors demonstrate by creating a CSV parser.
The relevant chapter 16: Using Parsec is available online; check the section titled Extended Example: Full CSV Parser.