如何使用Attoparsec解析雅虎历史csv

莫耶斯

我是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;
           .... }
马克斯·塔尔迪金

您可以使用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"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用attoparsec解析IP地址

来自分类Dev

使用Attoparsec解析简单的分子名称

来自分类Dev

使用Attoparsec解析简单的分子名称

来自分类Dev

如何使用pandas_datareader在没有雅虎或谷歌金融的情况下在我的脚本中加载历史股票指数数据?

来自分类Dev

使用attoparsec解析逗号内的逗号分隔字符串

来自分类Dev

Haskell:如何使用attoparsec以便从ByteString读取嵌套列表

来自分类Dev

Haskell:如何使用attoparsec以便从ByteString中读取嵌套列表

来自分类Dev

如何在 attoparsec 中使用带有结束字符的 sepBy?

来自分类Dev

如何使用PapaParse从文件中解析CSV?

来自分类Dev

如何使用流解析节点中的CSV?

来自分类Dev

如何使用bash解析csv文件

来自分类Dev

如何从Coinmarketcap解析历史BTC数据?

来自分类Dev

如何使用Python的CSV DictReader解析CSV输出?

来自分类Dev

如何检索雅虎搜索结果?

来自分类Dev

如何使用Nightwatch追溯历史?

来自分类Dev

使用 MechanicalSoup 登录雅虎财经

来自分类Dev

如何从GitHub解析CSV?

来自分类Dev

了解attoparsec解析器monad

来自分类Dev

基本的Attoparsec解析仅返回“ Right []”

来自分类Dev

基本的Attoparsec解析仅返回“ Right []”

来自分类Dev

使用骆驼解析CSV

来自分类Dev

如何使用批处理脚本解析csv文件?

来自分类Dev

如何使用Spring Batch解析CSV(包含逗号的值)

来自分类Dev

如何使用Papa Parse解析CasperJS中的CSV?

来自分类Dev

如何使用列名将值解析为csv

来自分类Dev

如何使用Spring Batch解析CSV(包含逗号的值)

来自分类Dev

如何使用php解析csv文件中的多行

来自分类Dev

如何使用python pandas将CSV解析为所需的格式?

来自分类Dev

如何使用opencsv编写java csv解析器

Related 相关文章

热门标签

归档