-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSV_Reader.hs
39 lines (34 loc) · 1.35 KB
/
CSV_Reader.hs
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
32
33
34
35
36
37
38
39
module CSV_Reader where
import Control.Exception
import System.IO
import Data.Char ( isSpace )
--import Data.Text
import System.Directory
fileReadCsv :: String -> IO [[String]]
fileReadCsv str = do
b <- doesFileExist st
if b then (do read) else error ("Error - CSV file name \"" ++ st ++ "\" not found!") -- error check if file is not found
where st = removeQuotes str ++ ".csv" -- removes quotes from parsing and adds .csv
b = doesFileExist st
read = fileReadCsv' st
fileReadCsv' :: String -> IO [[String]]
fileReadCsv' str = do
text <- readFile str
let ls = lines text
let cells = map (splitOn ',') ls
let nowhitespace = map (map removeWhitespace) cells
return nowhitespace
--"example" -> example
removeQuotes :: String -> String
removeQuotes str = drop 1 (take (length str-1) str)
--" example " -> "example"
removeWhitespace :: String -> String
removeWhitespace str = reverse (dropWhile isSpace (reverse (dropWhile isSpace str)))
--SplitOn function from lectures
splitOn :: Char -> String -> [String]
splitOn c [] = []
splitOn c ls = takeWhile (/=c) ls : splitOn' c (dropWhile (/=c) ls)
where splitOn' c [] = []
splitOn' c [x] | x==c = [[]]
splitOn' c (x:xs) | x==c = splitOn c xs
| otherwise = []