Simple configurable PureScript CSV parser.
Based on purescript-parsing. Parse list of rows (getting a simple list of fields) or using header row (getting
a map from field name to value).
importPreludeimportText.Parsing.CSV (defaultParsers, makeParsers)
importText.Parsing.Parser (runParser)
importData.ListimportData.Eitherlet isTrue exp = either (\_->false) (== (exp ::List (ListString)))
let testFile = "a,,c,\n,1,2,3\n\"x\",\"field,quoted\",z\n"::Stringlet testResult = toList $ toList <$> [["a", "", "c", ""], ["", "1", "2", "3"], ["x", "field,quoted", "z"], [""]]let parseResult = runParser testFile defaultParsers.file
parseResult
isTrue testResult parseResult
:t runParser testFile defaultParsers.fileIf you want to create your own parsers using different field separators, different quotation chars and different line endings (instead of using the defaultParsers) you can do that by
excelParsers = makeParsers '\''";""\r\n"This will generate a value of type
typeParsersa=
{
quoted:: (Pa->Pa),
chars::PString,
qchars::PString,
field::PString,
row::P (ListString),
file::P (List (ListString)),
fileHeaded::P (List (M.MapStringString))
}In this specific case using ' as a quotation character a ; as a field seperator and using windows \r\n line seperators instead of the *nix \n.
The easiest and fastest way of course is just to use the defaultParsers who are defined as defaultParsers = makeParsers '"' "," "\n".