Fully featured CSV parser for Haskell?

后端 未结 5 1672
长发绾君心
长发绾君心 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:33

    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.

    0 讨论(0)
  • 2021-02-07 05:38

    Cassava works in memory and is very simple library e.g.

    encode [("John" :: Text, 27), ("Jane", 28)]
    "John,27\r\nJane,28\r\n"
    
    0 讨论(0)
  • 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'.

    0 讨论(0)
  • 2021-02-07 05:48

    A quick search on Hackage finds Data.Spreadsheet, which does have customizable quote and separator.

    0 讨论(0)
  • 2021-02-07 05:52

    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.

    0 讨论(0)
提交回复
热议问题