Giter Site home page Giter Site logo

paultopia / semantic-csv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from metasoarous/semantic-csv

0.0 3.0 0.0 724 KB

Higher level tools for working with CSV data and files

Home Page: http://metasoarous.github.io/semantic-csv

License: Eclipse Public License 1.0

Shell 0.13% Clojure 99.87%

semantic-csv's Introduction

Semantic CSV

Gitter

A Clojure library with higher level CSV parsing/processing functionality.

The two most popular CSV parsing libraries for Clojure presently - clojure/data.csv and clojure-csv - concern themselves only with the syntax of CSV; They take CSV text, transform it into a collection of vectors of string values, and that's it. Semantic CSV takes the next step by giving you tools for addressing the semantics of your data, helping you put it in a form that better reflects what it represents.

See the introductory blog post for more information on the design phylosophy.

Features

  • Absorb header row as a vector of column names, and return remaining rows as maps of column-name -> row-val
  • Write from a collection of maps, given a header
  • Apply casting/formatting functions by column name, while reading or writing
  • Remove commented out lines (by default, those starting with #)
  • Compatible with any CSV parsing library returning/writing a sequence of row vectors
  • (SOON) A "sniffer" that reads in N lines, and uses them to guess column types

Structure

Semantic CSV is structured around a number of composable processing functions for transforming data as it comes out of or goes into a CSV file. This leaves room for you to use whatever parsing/formatting tools you like, reflecting a nice decoupling of grammar and semantics. However, a couple of convenience functions are also provided which wrap these individual steps in an opinionated but customizable manner, helping you move quickly while prototyping or working at the REPL.

Installation

Assuming you're using Leiningen, just add the following to your dependencies:

Clojars Project

If you'd like to use a specific commit, you can also use lein-git-deps to do so by adding the followingto your project.clj:

:plugins [[lein-git-deps "0.0.1-SNAPSHOT"]]
:git-dependencies [["https://github.com/metasoarous/semantic-csv.git"]]
:source-paths ["src/<yuourproject>", ".lein-git-deps/semantic-csv/src/"]

Then run lein git-deps and you should be good to go.

Usage

Please see metasoarous.github.io/semantic-csv for complete documentation.

Semantic CSV emphasizes a number of individual processing functions which can operate on the output of a syntactic csv parser such as clojure.data.csv or clojure-csv. This reflects a nice decoupling of grammar and semantics, in an effort to make this library as composable and interoperable as possible.

=> (require '[clojure.java.io :as io]
            '[clojure-csv.core :as csv]
            '[semantic-csv.core :as sc :refer :all])
=> (with-open [in-file (io/reader "test/test.csv")]
     (->>
       (csv/parse-csv in-file)
       remove-comments
       mappify
       (cast-with {:this ->int})
       doall))

({:this 1, :that "2", :more "stuff"}
 {:this 2, :that "3", :more "other yeah"})

However, some opinionated, but configurable convenience functions are also provided.

(with-open [in-file (io/reader "test/test.csv")]
  (doall
    (process (parse-csv in-file))))

And for the truly irreverent... (who don't need computer laziness):

(slurp-csv "test/test.csv")

Writing CSV data

As with the input processing functions, the writer processing functions come in modular peices you can use as you see fit. This time let's use clojure/data.csv:

(require '[clojure.data.csv :as cd-csv])

(def data [{:this 1, :that "2", :more "stuff"}
           {:this 2, :that "3", :more "other yeah"}])

(with-open [out-file (io/writer "test.csv")]
  (->> data
       (cast-with {:this #(-> % float str)})
       vectorize
       (cd-csv/write-csv out-file)))

And again, as with the input processing functions, here we also provide a quick and dirty, opinionated convenience function for automating some of this:

(spit-csv "test2.csv"
          {:cast-fns {:this #(-> % float str)}}
          data)

And there you have it.

Contributing

Feel free to submit a pull request. If you're looking for things to help with, please take a look at the GH issues page. Contributing to the issues with comments, feedback, or requests is also greatly appreciated.

License

Copyright © 2014 Christopher Small

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

semantic-csv's People

Contributors

metasoarous avatar jonyepsilon avatar alphaho avatar dhruvbhatia avatar gitter-badger avatar

Watchers

James Cloos avatar Paul Gowder 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.