Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

haskell - how to parse yahoo csv with parsec

how to parse into array such as open[i],high[i],low[i],close[i]

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

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

testhaskell.hs:22:19:
    Couldn't match expected type `String -> a0'
                with actual type `Text.Parsec.Prim.ParsecT
                                    String () Data.Functor.Identity.Identity [String]'

import System.IO 
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString as Str
import Text.ParserCombinators.Parsec

word    :: Parser String
word    = many1 letter

sentence    :: Parser [String]
sentence    = do{ words <- sepBy1 word separator
                ; oneOf ".?!" <?> "end of sentence"
                ; return words
                }

separator   :: Parser ()
separator   = skipMany1 (space <|> char ',' <?> "")

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

update:

module Main where

import qualified Data.ByteString.Char8          as B
import           Data.Map                       ((!))
import           Data.Text
import qualified Data.Vector                    as V
import           System.Directory
import           Test.Framework                 (Test, defaultMain, testGroup)
import           Test.Framework.Providers.API
import           Test.HUnit                     ((@=?))

import           Data.CSV.Conduit


main :: IO ()
main = defaultMain tests


tests :: [Test]
tests = [testGroup "Basic Ops" baseTests]


baseTests :: [Test]
baseTests =
  [ 
    testCase "simple parsing works" test_simpleParse
  ]


test_simpleParse :: IO ()
test_simpleParse = do
  (d :: V.Vector (MapRow B.ByteString)) <- readCSVFile csvSettings testFile1
  V.mapM_ assertRow d
  where
    assertRow r = v3 @=? (v1 + v2)
      where v1 = readBS $ r ! "Open"
            v2 = readBS $ r ! "High"
            v3 = readBS $ r ! "Low"
            v4 = readBS $ r ! "Close"


csvSettings :: CSVSettings
csvSettings = defCSVSettings { csvQuoteChar = Just '`'}

testFile1 :: FilePath
testFile1 = "C:\Users\ivan\Downloads\0005.HK.csv"


readBS :: B.ByteString -> Int
readBS = read . B.unpack

testhaskell.hs:52:5: Not in scope: `testCase'

testhaskell.hs:58:9: Illegal type signature: `V.Vector (MapRow B.ByteString)' Perhaps you intended to use -XScopedTypeVariables In a pattern type-signature

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd strongly recommend you not do this. There are a number of high-quality CSV libraries on Hackage, and rolling your own is a recipe of problems. At FP Complete, we use csv-conduit, though cassava is also a great library. I'd recommend you try out one of them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...