Fully featured CSV parser for Haskell?

后端 未结 5 1739
长发绾君心
长发绾君心 2021-02-07 05:26

Can anybody recommend a way to parse CSV files with options to:

  • set cells/fields separator
  • set end of record/row terminator
  • set quote-character f
5条回答
  •  没有蜡笔的小新
    2021-02-07 05:40

    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'.

提交回复
热议问题