forked from solresol/padiclinear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsvReader.lhs
31 lines (31 loc) · 1.31 KB
/
CsvReader.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
> module CsvReader where
> import Data.Maybe
>
> data CSVRow = CSVRow [String] deriving (Eq, Ord, Show)
> data CSVHeader = CSVHeader [String] deriving (Eq, Ord, Show)
> data CSVFile = CSVFile CSVHeader [CSVRow] deriving (Eq, Ord, Show)
> getColumn :: String -> CSVHeader -> CSVRow -> String
> getColumn heading (CSVHeader (headers)) (CSVRow datarow)
> | isJust result = fromJust result
> | otherwise = error ("There is no header " ++ heading ++ " in the heading list " ++ (show headers))
> where result = lookup heading (zip headers datarow)
> getColumnList :: [String] -> CSVHeader -> CSVRow -> [String]
> getColumnList [] _ _ = []
> getColumnList (h:hs) ch cr = (getColumn h ch cr) : (getColumnList hs ch cr)
>
> parseCSVline :: String -> Char -> [String]
> parseCSVline content separator
> | r == "" = [l]
> | otherwise = l : (parseCSVline (tail r) separator)
> where (l, r) = span (\t -> t /= separator) content
>
> parseCSVfile :: String -> CSVFile
> parseCSVfile contents = CSVFile heading body
> where chunkedContent = [ parseCSVline line '\t' | line <- lines contents ]
> heading = CSVHeader (head chunkedContent)
> body = [ CSVRow x | x <- tail chunkedContent ]
>
> readCSVfile :: String -> IO CSVFile
> readCSVfile filename = do
> contents <- readFile filename
> return (parseCSVfile contents)