簡體   English   中英

haskell中的木薯解析錯誤

[英]Cassava parsing error in haskell

我試圖使用木薯將csv轉換為載體。 嘗試轉換的csv是fischer虹膜數據集,用於機器學習。 它由四個雙打和一個字符串組成。 我的代碼如下:

{-# 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

當我運行它時,似乎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

但是我收到以下錯誤:

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)

有沒有人知道為什么我會收到這個錯誤? csv沒有缺失值,如果我替換產生另一行錯誤的行,我會得到相同的錯誤。

您的數據似乎沒有標頭,由decodeByName假設

假設數據前面有標題。

添加標題,或使用decode NoHeaderFromRecord類型類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM