Giter VIP home page Giter VIP logo

ezcsv's Introduction

ezcsv

Convert between CSV and JSON types with very simple API.

NPM info

Travis build status

Install

npm install ezcsv

API

This library tries to convert tables stored as CSV or TSV into an array of json objects. Consider a table of the following format:

Name Date
Yorkan, Yorkie Sun Aug 31 2014 16:07:57 GMT-0700 (PDT)
Yorkie, Yorkan Sun Aug 31 2014 16:08:23 GMT-0700 (PDT)

This module would convert that to the following JSON structure:

{
  "header": [
    "Name",
    "Date"
  ],
  "items": [
    {
      "Name": "Yorkan, Yorkie",
      "Date": "Sun Aug 31 2014 16:07:57 GMT-0700 (PDT)"
    },
    {
      "Name": "Yorkie, Yorkan",
      "Date": "Sun Aug 31 2014 16:08:23 GMT-0700 (PDT)"
    }
  ]
}

Read TSV

If a file is saved as TSV, the module expects the first line to be the 'header'. It then creates an object for every subsequent line.

    var ezcsv = require('ezcsv');
    var json = ezcsv.tsv.read('example1.tsv');
    ....
    json.items.forEach(function () { .... })

Writing TSV as output

You can write a set of objects to TSV. If the header field is omitted, it will calculate the header based on the values of the objects provided (default to empty string for any missing fields).

    var ezcsv = require('ezcsv');
    var json = ezcsv.tsv.write(
      'example1.tsv', 
      {
        header: ['Name', 'Date'], // optional, it will auto-calculate
        items: [ .... ]
      }
    );

CSV Files

Reading and writing CSV files is similar, just use ezcsv.csv instead of ezcsv.tsv/

Empty lines, separators etc.

By default, empty lines are skipped both when reading and writing. Line separator is newline when writing and also carriage-return when reading. Field seperator is comma or tab as the case may be. All of these are configurable by using the get method with a config (it returns the read and write methods). Look at index.js to see how to specify these configuration parameters.

ezcsv's People

Contributors

like-falling-leaves avatar

Stargazers

Bernhard Gschwantner avatar

Watchers

James Cloos avatar  avatar

Forkers

yamilquery

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.