Git Product home page Git Product logo

csvparser's Introduction

CSV Parser Build Status codecov.io

Simple library that parse a CSV file and returns an array of pointers to the given type.

Limitations

  • Works only with a struct that contains string, int, uint, bool, time and float fields.

Getting started

Install

go get -u github.com/empatica/csvparser

Usage

Define your struct:

type YourStruct struct{
  Field1 string
  Field2 int
  Field3 bool
  Field4 float64
  Field5 time.Time `csvDate:"2006-05-07"`
}

If you don't add 'csv' tags close to each struct's field, the lib will set the first field using the first column of csv's row, and so on. So the previous struct is the same as:

type YourStruct struct{
  Field1 string    `csv:"0"`
  Field2 int       `csv:"1"`
  Field3 bool      `csv:"2"`
  Field4 float64   `csv:"3"`
  Field5 time.Time `csv:"4" csvDate:"2006-05-07"`
}

You can always define 'csv' tags (for all or some of the struct's fields) that will tell the lib which column to use:

type YourStruct struct{
  Field1 string    `csv:"4"`
  Field2 int       `csv:"0"`
  Field3 bool      `csv:"3"`
  Field4 float64   `csv:"2"`
  Field5 time.Time `csv:"1" csvDate:"2006-05-07"`
}
Note for time.Time fields:

It's required to specify a csvDate tag that will be used for parsing, following the rules describere here

Parse the file:

var csvParser = parser.CsvParser{
    CsvFile:      "path_to_your_file.csv",
    CsvSeparator: ',',
    SkipFirstLine : true, //default:false
    SkipEmptyValues : true, //default:false. It will skip empty values and won't try to parse them
}

var parsedItems, err = csvParser.Parse(YourStruct{})

for i:=0;i<len(parsedItems);i++{
  log.Print(parsedItems[i].(*YourStruct))
}

csvparser's People

Contributors

faustoempatica avatar alecha avatar uwe avatar blaircamp avatar

Watchers

James Cloos 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.