如何用parsec解析yahoo csv [英] how to parse yahoo csv with parsec
问题描述
如何解析数组,例如open [i],high [i],low [i],close [i]
testhaskell.hs:22:5:
与`IO'类型'[]'不匹配'
期望类型:IO a0
实际类型:[a0]
在`map'调用的返回类型中
在'do'块的语句中:map(\ line - >句子行)allLines
在表达式中:
do {handle < - openFile
C:\\ Users \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
内容< - hGetContents句柄;
let allLines =行内容;
map(\ line - >句子行)allLines;
....}
testhaskell.hs:22:19:
无法匹配期望的类型`String - > a0'
与实际类型`Text.Parsec.Prim.ParsecT
String()Data.Functor.Identity.Identity [String]'
$ b $
import System.IO
导入限定数据。 ByteString.Char8 as BS
将合格的Data.ByteString导入为Str
import Text.ParserCombinators.Parsec
word :: Parser String
word = many1 letter
sentence:Parser [String]
sentence = do {words< - sepBy1 word separator
; oneOf。?! <?> 句尾
;返回单词
separator :: Parser()
separator = skipMany1(space< |> char','<>)
main = do
handle < - openFileC:\\ Users \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\内容< - hGetContents句柄
let allLines =行内容
map(\ line - >句子行)allLines
--putStr内容
hClose句柄
更新:
模块Main其中
将合格的Data.ByteString.Char8导入为B
导入Data.Map((!))
导入Data.Text
将合格的Data.Vector导入为V
import System.Directory
import Test.Framework(Test,defaultMain,testGroup)
import Test.Framework.Providers.API
import Tes t.HUnit((@ =?))
import Data.CSV.Conduit
$ b $ main :: IO()
main = defaultMain测试
tests :: [Test]
tests = [testGroupBasic OpsbaseTests]
baseTests :: [Test]
baseTests =
[
testCasesimple parsing workstest_simpleParse
]
test_simpleParse :: IO()
test_simpleParse = do
(d :: V.Vector(MapRow B.ByteString))< - readCSVFile csvSettings testFile1
V.mapM_ assertRow d
其中
assertRow r = v3 @ = ? (v1 + v2)
其中v1 = readBS $ r! 打开
v2 = readBS $ r! 高
v3 = readBS $ r! 低
v4 = readBS $ r! Close
csvSettings :: CSVSettings
csvSettings = defCSVSettings {csvQuoteChar =只是'`'}
testFile1 :: FilePath
testFile1 =C:\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' ; Int
readBS = read。 B.unpack
testhaskell.hs:52:5:不在范围内:`testCase'
testhaskell.hs:58:9:
非法类型签名:`V.Vector(MapRow B.ByteString)'
也许您打算使用-XScopedTypeVariables
在模式类型签名中
我强烈建议您不要做这个。 Hackage上有许多高质量的CSV库,并且自己推出是一个问题。在FP完成时,我们使用 csv-conduit ,但木薯也是一个很棒的图书馆。我建议你尝试一下。
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
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.
这篇关于如何用parsec解析yahoo csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!