使用csv-conduit将CSV文件解析为自定义数据类型

时间:2017-02-13 01:57:39

标签: csv haskell conduit

我对如何将csv-conduit与自定义数据类型一起使用感到困惑。我想拿一排股票数据,比如这个:

Date,Open,High,Low,Close,Volume,Adj Close
2017-02-10,2312.27002,2319.22998,2311.100098,2316.100098,3475020000,2316.100098

并将其解析为我在下面的MWE中声明的StockInfo类型。我从我需要的文档中收集了StockInfo FromNamedRecordToNamedRecordCSV ByteString的实例。我相信我已经为前两个做了,但我不明白如何为CSV ByteString实现必要的方法。任何帮助将不胜感激。

MWE:

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}

module Lib
    ( readStocks
    ) where

import           Data.ByteString
import           Data.Conduit
import           Data.Conduit.Binary
import           Data.Conduit.List           as CL
import           Data.CSV.Conduit
import           Data.CSV.Conduit.Conversion
import           Data.Text                   (Text)
import           Data.Vector
import           System.IO

readStocks :: FilePath -> IO (Vector StockInfo)
readStocks fp = readCSVFile defCSVSettings fp

data StockInfo = StockInfo
  { date     :: !String
  , open     :: !Double
  , high     :: !Double
  , low      :: !Double
  , close    :: !Double
  , volume   :: !Integer
  , adjClose :: !Double
  }

instance FromNamedRecord StockInfo where
  parseNamedRecord m =
    StockInfo <$>
    m .: "Date" <*>
    m .: "Open" <*>
    m .: "High" <*>
    m .: "Low" <*>
    m .: "Close" <*>
    m .: "Volume" <*>
    m .: "Adj Close"

instance ToNamedRecord StockInfo where
  toNamedRecord (StockInfo date open high low close volume adjClose) =
    namedRecord [ "Date" .= date
                , "Open" .= open
                , "High" .= high
                , "Low" .= low
                , "Close" .= close
                , "Volume" .= volume
                , "Adj Close" .= adjClose
                ]

instance CSV ByteString StockInfo where
    -- rowToStr = undefined
    -- intoCSV = undefined
    -- fromCSV = undefined

1 个答案:

答案 0 :(得分:5)

这是我应该做的:

{-# LANGUAGE OverloadedStrings     #-}

module Lib
    ( readStocks
    ) where

import           Data.ByteString
import           Data.Conduit
import           Data.Conduit.Binary
import           Data.CSV.Conduit
import           Data.CSV.Conduit.Conversion
import           Data.Text                   (Text)
import           Data.Vector
import           System.IO

readStocks :: FilePath -> IO (Vector (Named StockInfo))
readStocks fp = readCSVFile defCSVSettings fp

data StockInfo = StockInfo
  { date     :: !String
  , open     :: !Double
  , high     :: !Double
  , low      :: !Double
  , close    :: !Double
  , volume   :: !Integer
  , adjClose :: !Double
  } deriving (Show, Eq, Read)

instance FromNamedRecord StockInfo where
  parseNamedRecord m =
    StockInfo <$>
    m .: "Date" <*>
    m .: "Open" <*>
    m .: "High" <*>
    m .: "Low" <*>
    m .: "Close" <*>
    m .: "Volume" <*>
    m .: "Adj Close"

instance ToNamedRecord StockInfo where
  toNamedRecord (StockInfo date open high low close volume adjClose) =
    namedRecord [ "Date" .= date
                , "Open" .= open
                , "High" .= high
                , "Low" .= low
                , "Close" .= close
                , "Volume" .= volume
                , "Adj Close" .= adjClose
                ]