Git Product home page Git Product logo

purescript-csv's Introduction

purescript-csv

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).

Example (copy into your REPL)

import Prelude

import Text.Parsing.CSV (defaultParsers, makeParsers)
import Text.Parsing.Parser (runParser)

import Data.List
import Data.Either

let isTrue exp = either (\_-> false) (== (exp :: List (List String)))

let testFile = "a,,c,\n,1,2,3\n\"x\",\"field,quoted\",z\n" :: String
let 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.file

If 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

type Parsers a =
{
  quoted :: (P a -> P a),
  chars :: P String,
  qchars :: P String,
  field :: P String,
  row :: P (List String),
  file :: P (List (List String)),
  fileHeaded :: P (List (M.Map String String))
}

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".

purescript-csv's People

Contributors

jez avatar nwolverson avatar robkuz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.