Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more
Lensy interface for parsing CSV's
Haskell
Branch: master
Clone or download
Latest commit 1bcac70 Feb 2, 2020
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
data
src/Data/Csv Add docs and 'cassavaUnnamed' 'cassavaNamed' Feb 2, 2020
test Initial commit Nov 26, 2019
.gitignore
ChangeLog.md Initial commit Nov 26, 2019
LICENSE Initial commit Nov 26, 2019
README.md Update README Feb 2, 2020
Setup.hs Initial commit Nov 26, 2019
optics-by-example.png Update README Feb 2, 2020
package.yaml Add doc tests Feb 2, 2020
stack.yaml Initial commit Nov 26, 2019
stack.yaml.lock

README.md

lens-csv

If you enjoy working with lenses (or need a hand learning how they work) my book Optics By Example is a great place to learn more!

Optics By Example

A lensy layer on top of Cassava which affords streaming, traversable, CSV parsing.

Still experimental (but working). Please file an issue if there are features the library doesn't support.

Example:

>>> import Data.ByteString.Lazy as BL
>>> myCsv <- BL.readFile "./data/simple.csv"
>>> myCsv ^.. namedCsv . taking 2 rows . column @String "state_code" 
[ "NY"
, "CA"
]

>>> myCsv ^.. namedCsv . taking 2 rows . _NamedRecord @(M.Map String String)
[ fromList [("population","19540000"), ("state_code","NY")]
, fromList [("population","39560000"), ("state_code","CA")]
]

-- For csv files without headers
>>> myCsv ^.. csv . taking 2 rows . _Record @[String]
[ ["state_code", "population"]
, ["NY"        , "19540000"]
]

-- 'column' infers whether it's a named or unnamed csv and accepts the appropriate index type (either ByteString or Int)
>>> myCsv ^.. csv . rows . column @Int 1
[19540000,39560000]

-- Use traversals to edit cells 'in-place' (add 1337 to California's population)
>>> BL.putStrLn $ myCsv & namedCsv . row 1 . column @Int "population" +~ 1337
state_code,population
NY,19540000
CA,39561337
You can’t perform that action at this time.