-2

我是haskell的初学者,如何用attoparsec解析成开放数组、高数组等

module CsvParser (
      Quote (..)
    , csvFile
    , quote
    ) where
import System.IO
import Data.Attoparsec.Text
import Data.Attoparsec.Combinator
import Data.Text (Text, unpack)
import Data.Time
import System.Locale
import Data.Maybe

data Quote = Quote {
        qTime       :: LocalTime,
        qAsk        :: Double,
        qBid        :: Double,
        qAskVolume  :: Double,
        qBidVolume  :: Double
    } deriving (Show, Eq)

csvFile :: Parser [Quote]
csvFile = do
    q <- many1 quote
    endOfInput
    return q

quote   :: Parser Quote
quote   = do
    time        <- qtime
    qcomma
    ask         <- double
    qcomma
    bid         <- double
    qcomma
    askVolume   <- double
    qcomma
    bidVolume   <- double
    endOfLine
    return $ Quote time ask bid askVolume bidVolume 

qcomma  :: Parser ()
qcomma  = do 
    char ','
    return ()

qtime   :: Parser LocalTime
qtime   = do
    tstring     <- takeTill (\x -> x == ',')
    let time    = parseTime defaultTimeLocale "%d.%m.%Y %H:%M:%S%Q" (unpack tstring)
    return $ fromMaybe (LocalTime (fromGregorian 0001 01 01) (TimeOfDay 00 00 00 )) time

--testString :: Text
--testString = "01.10.2012 00:00:00.741,1.28082,1.28077,1500000.00,1500000.00\n" 

quoteParser = parseOnly quote

main = do  
    handle <- openFile "C:\\Users\\ivan\\Downloads\\0005.HK.csv" ReadMode  
    contents <- hGetContents handle  
    let allLines = lines contents
    map (\line -> quoteParser line) allLines
    --putStr contents  
    hClose handle

错误信息:

testhaskell.hs:89:5:
    Couldn't match type `[]' with `IO'
    Expected type: IO (Either String Quote)
      Actual type: [Either String Quote]
    In the return type of a call of `map'
    In a stmt of a 'do' block:
      map (\ line -> quoteParser line) allLines
    In the expression:
      do { handle <- openFile
                       "C:\\Users\\ivan\\Downloads\\0005.HK.csv" ReadMode;

           contents <- hGetContents handle;
           let allLines = lines contents;
           map (\ line -> quoteParser line) allLines;
           .... }

testhaskell.hs:89:37:
    Couldn't match type `[Char]' with `Text'
    Expected type: [Text]
      Actual type: [String]
    In the second argument of `map', namely `allLines'
    In a stmt of a 'do' block:
      map (\ line -> quoteParser line) allLines
    In the expression:
      do { handle <- openFile
                       "C:\\Users\\ivan\\Downloads\\0005.HK.csv" ReadMode;

           contents <- hGetContents handle;
           let allLines = lines contents;
           map (\ line -> quoteParser line) allLines;
           .... }
4

2 回答 2

2

该错误与 parsec 或 attoparsec 无关。错误消息指向的行不是一个IO操作,因此当您尝试将其用作一个操作时会导致错误:

main = do  
    handle <- openFile "C:\\Users\\ivan\\Downloads\\0005.HK.csv" ReadMode  
    contents <- hGetContents handle  
    let allLines = lines contents
    map (\line -> quoteParser line) allLines   -- <== This is not an IO action
    --putStr contents  
    hClose handl

你忽略了map调用的结果。你应该将它存储在一个变量中let,就像你对 的结果一样lines

第二个错误是因为您尝试使用Textas Stringwhich 是不同的类型,即使它们都表示字符的有序集合(它们也有不同的内部表示)。pack您可以使用和在这两种类型之间进行转换unpackhttp ://hackage.haskell.org/package/text/docs/Data-Text.html#g:5

此外,您应该始终明确给出main类型签名main :: IO ()。如果你不这样做,它有时会导致微妙的问题。

不过,正如其他人所说,您可能应该使用 csv 解析器包。

于 2014-04-22T16:23:03.470 回答
0

您可以使用attoparsec-csv包,也可以查看它的源代码以了解如何自己编写它。

代码会像

import qualified Data.Text.IO as T
import Text.ParseCSV

main = do
  txt <- T.readFile "file.csv"
  case parseCSV txt of
    Left  err -> error err
    Right csv -> mapM_ (print . mkQuote) csv

mkQuote :: [T.Text] -> Quote
mkQuote = error "Not implemented yet"
于 2014-04-22T09:25:05.183 回答