I have written a parsec code which works perfectly for what I want. It parses as expected the following file:
4,5
6,7
The corresponding code is like this:
import Text.ParserCombinators.Parsec
import Control.Applicative hiding ((<|>))
import Control.Monad
data Test = Test Integer Integer deriving Show
integer :: Parser Integer
integer = rd <$> many1 digit
where rd = read :: String -> Integer
testParser :: Parser Test
testParser = do
a <- integer
char ','
b <- integer
return $ Test a b
testParserFile = endBy testParser eol
eol :: Parser Char
eol = char '\n'
main = do
a <- parseFromFile testParserFile "./jack.txt"
print a
But my actual files are like this:
col 1,col 2
4,5
6,7
Is there a way to make the above parser, just skip the first line ?
testParserFile = manyTill anyChar newline *> endBy testParser eol
manyTill p end
applies p until end
succeeds. *>
sequences two actions and discards the first value.
Note: if your actual file doesn't contain a newline at the end, then you need to use sepEndBy
instead of endBy
. However, this could be a result of the Markdown parser on StackOverflow.