Skip to content

Instantly share code, notes, and snippets.

@Zetzumarshen
Created May 18, 2017 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zetzumarshen/1ec8b45d7f82f3b6e4803f3383949055 to your computer and use it in GitHub Desktop.
Save Zetzumarshen/1ec8b45d7f82f3b6e4803f3383949055 to your computer and use it in GitHub Desktop.
CSV Parser using parsec/haskell
import Text.ParserCombinators.Parsec
import System.IO
csvFile = endBy line eol
line = sepBy cell (char ',')
cell = many (noneOf ",\n")
eol = char '\n'
parseCSV :: String -> Either ParseError [[String]]
parseCSV input = parse csvFile "(unknown)" input
main = do
handle <- openFile "test.csv" ReadMode
contents <- hGetContents handle
case parseCSV contents of
Left x -> print x
Right results -> print results
hClose handle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment