Haskell csv-conduit in GHCi

I was offered csv-conduit as a good Haskell package for working with CSV files. I want to know how this works, but the documentation is too complicated for a Haskell programmer for beginners.

Is there any way to find out how this works by trial and error in GHCi?

In particular, should I load modules and files from GHCi or write a simple HS file to load them, and then navigate interactively?


I mentioned csv-conduit, but I'm open to using any CSV package. I just need to pull myself together and trick him until I feel at ease (as I would do in IDLE).

+5
source share
3 answers

Take a look at the following function: readCSVFile :: :: (MonadResource m, CSV ByteString a) => CSVSettings -> FilePath -> m [a]

, CSVSettings, defCSVSettings a FilePath (aka String), "file.csv" - .

, (MonadResource m, CSV ByteString a). , . IO , MonadResource m, m ResourceT IO, MonadBaseControl IO, runResourceT. conduit .

CSV ByteString a , CSV. http://hackage.haskell.org/packages/archive/csv-conduit/0.2.1.1/doc/html/Data-CSV -Conduit.html # t: CSV ( typeclass...) "", , CSV ByteString a. : CSV ByteString ByteString CSV ByteString Text.

Text , unicode, CSV . ByteString a [Word8], Text [Char], , , . , a Text ( ByteString ).

, ResourceT IO [Row Text]. , ResourceT , "" runResourceT. ,

readFile :: FilePath -> IO [Row Text]
readFile = runResourceT . readCSVFile defCSVSettings

, , , [Row Text], map fold, .

GHCI . , ; , CSVSettings FilePath, readCSVFile , m MonadResource m a CSV ByteString a, , GHCi, .

+5

Text.CSV? , Haskell, . , GHCi, .

+2

This works with the latest version of the csv-conduit package (version 0.6.3). Pay attention to the readCsv signature, without which I could not compile.

{-# LANGUAGE OverloadedStrings #-}

import Data.CSV.Conduit
import Data.Text (Text)
import qualified Data.Vector as V
import qualified Data.ByteString as B

csvset :: Char ->  CSVSettings 
csvset c =  CSVSettings {csvSep  = c, csvQuoteChar = Just '"'}

readCsv :: String -> Char -> IO (V.Vector (Row Text))
readCsv fp del = readCSVFile (csvset del) fp

main = readCsv "C:\\mydir\\myfile.csv" ';'
0
source

All Articles