简体   繁体   English

haskell中的木薯解析错误

[英]Cassava parsing error in haskell

Im trying to convert a csv into a vector using cassava. 我试图使用木薯将csv转换为载体。 The csv Im trying to convert is the fischer iris data set, used for machine learning. 尝试转换的csv是fischer虹膜数据集,用于机器学习。 It consists of four doubles and one string. 它由四个双打和一个字符串组成。 My code is the following: 我的代码如下:

{-# LANGUAGE OverloadedStrings #-}

module Main where
import Data.Csv
import qualified Data.ByteString.Lazy as BS
import qualified Data.Vector as V

data Iris = Iris
  { sepal_length  :: !Double
  , sepal_width   :: !Double
  , petal_length  :: !Double
  , petal_width   :: !Double
  , iris_type     :: !String
 } deriving (Show, Eq, Read)

instance FromNamedRecord Iris where
  parseNamedRecord r =
    Iris
      <$> r .: "sepal_length"
      <*> r .: "sepal_width"
      <*> r .: "petal_length"
      <*> r .: "petal_width"
      <*> r .: "iris_type"

printIris :: Iris -> IO ()
printIris r  = putStrLn $  show (sepal_length r) ++ show (sepal_width r)
   ++ show(petal_length r) ++ show(petal_length r) ++ "hola"

main :: IO ()
main = do
  csvData <- BS.readFile "./iris/test-iris"
  print csvData
  case decodeByName csvData of
    Left err -> putStrLn err
    -- forM : O(n) Apply the monadic action to all elements of the vector,
    -- yielding a vector of results.
    Right (h, v) -> V.forM_ v $ printIris

When I run this, it seems as if the csvData is correctly formatted, the first lines from the print csvData return the following: 当我运行它时,似乎csvData格式正确,print csvData的第一行返回以下内容:

"5.1,3.5,1.4,0.2,Iris-setosa\n4.9,3.0,1.4,0.2,Iris- setosa\n4.7,3.2,1.3,0.2,Iris-setosa\n4.6,3.1,1.5,0.2,Iris-setosa\n5.0,3.6,1.4,0.2,Iris-setosa\n5.4,3.9,1.7,0.4,Iris-setosa\n4.6,3.4,1.4,0.3,Iris-setosa\n5.0,3.4,1.5,0.2,Iris-setosa\n4.4,2.9,1.4,0.2,Iris-setosa\n4.9,3.1,1.5,0.1,Iris-setosa\n5.4,3.7,1.5,0.2,Iris-setosa\n4.8,3.4,1.6,0.2,Iris-setosa\n4.8,3.0,1.4,0.1,Iris-setosa\n4.3,3.0,1.1,0.1,Iris-setosa\n5.8,4.0,1.2,0.2,Iris-setosa\n5.7,4.4,1.5,0.4,Iris-set

But I get the following error: 但是我收到以下错误:

parse error (Failed reading: conversion error: no field named "sepal_length")  at 
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4 (truncated)

Does anybody have any idea as to why I can be getting this error? 有没有人知道为什么我会收到这个错误? The csv has no missing values, and if I replace the line which produces the error for another row I get the same error. csv没有缺失值,如果我替换产生另一行错误的行,我会得到相同的错误。

It appears your data does not have a header, which is assumed by decodeByName 您的数据似乎没有标头,由decodeByName假设

The data is assumed to be preceeded by a header. 假设数据前面有标题。

Add a header, or use decode NoHeader and the FromRecord type class. 添加标题,或使用decode NoHeaderFromRecord类型类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM