Haskell Text.CSV模块 [英] Haskell Text.CSV module

查看:55
本文介绍了Haskell Text.CSV模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我要为 parseCSV 函数输入 Text.Parsec.Error.ParseError CSV ,尽管在​​文档中它说输出是 ParseError CSV?我想将CSV文件导入Haskell,然后从中导出特定的列,然后计算该列的统计信息.

Why do I get type Either Text.Parsec.Error.ParseError CSV for the parseCSV function although in the documentation it says that output is Either ParseError CSV? I want to import a CSV file into Haskell and then export specific column from it and then compute statistics for that column.

我导入CSV文件,例如:

I import a CSV file like:

data = parseCSV "/home/user/Haskell/data/data.csv"

noEmpRows = either (const []) (filter (\row -> 2 <= length row))

readIndex :: Read cell => Either a CSV -> Int -> [cell]

readIndex csv index = map (read . (!!index)) (noEmpRows csv)

,然后当我想 readIndex data 9 :: [Integer] 时出现错误.

and then I get an error when I want to readIndex data 9 :: [Integer].

我也尝试过一个函数 parseCSVFromFile .

https://hackage.haskell.org/package/csv-0.1.2/docs/Text-CSV.html#t:CSV

预先感谢您的帮助.

推荐答案

您似乎真正要问的问题是如何使用Text.CSV?

The question you really seem to be asking is How do I use Text.CSV?

给出文件test.csv:

Given the file test.csv:

1,Banana,17
2,Apple,14
3,Pear,21

以及GHCi中的这一行:

and this line in GHCi:

Prelude> Text.CSV.parseCSVFromFile "test.csv"
Right [["1","Banana","17"],["2","Apple","14"],["3","Pear","21"],[""]]

如果要提取列,则为此构建一个函数:

If you want to extract a column, then build a function for that:

main :: IO ()
main = do
  test_csv <- parseCSVFromFile "test.csv"
  case test_csv of
    Right csv -> print (extractColumn csv 2 :: [Int])
    Left err -> print err

extractColumn :: Read t => CSV -> Int -> [t]
extractColumn csv n =
  [ read (record !! n) | record <- csv
                       , length record > n
                       , record /= [""] ]

这应该产生输出 [17,14,21] .

因为这里有足够的失败空间(一行中的字段可能少于 n ,或者给定行中字段 n 中的字符串可能无法读取为键入 t ),则可能需要处理或报告是否发生错误.如果上面的代码包含的字段太少,则上面的代码会丢掉该行,如果该字段不是一个 Int ,则会抛出一个 Prelude.read:no parse .考虑 readEither <代码> readMaybe .

Since there is ample room for failure here (a line could contain fewer fields than n, or the string in field n on a given line could fail to read as type t), you may want to handle or report if errors occur. The code above just throws away the line if it contains too few fields and throws a Prelude.read: no parse if the field isn't an Int. Consider readEither or readMaybe.

这篇关于Haskell Text.CSV模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆