Coder Social home page Coder Social logo

commons-csv's Introduction

README

commons-csv is a small Java library to handle CSV data.

It comes without any dependencies to any external libraries - except JRE classes.

The project is licensed by MIT-License.

Note

de.jcup.commons was created based on idea of the Apache commons - with the focus on common things that don’t exist there. Unfortunately, I only now saw that there is already a similar library from Apache under the exact same name "commons-csv" (see https://commons.apache.org/proper/commons-csv/).

This was not intentional or deliberate. I will continue to use my library, but as I said, there is also a (probably good) alternative from Apache itself.

Installation

Gradle

Create a build.gradle file and add following dependencies:

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'de.jcup.commons:commons-csv:1.0.0'
}

Usage

Example 1

Create a CSV model and convert to CSV string
CSVModel model = new CSVModel("First name", "Last name", "Hobbies");(1)
model.addRow().(2)
    set("Last name", "Tregnaghi").
    set("First name", "Albert").(3)
    set("Hobbies", "Software development; Electronic games, Book reading");(4)

String csv = model.toCSVString();(5)

System.out.println(csv);
  1. Creates a CSV model with named columns ("First name", "Last name", "Hobbies"). Because no additional model configuration is done, the defaults are set to Unix-Linendings, default separator ; and cells will be automatically trimmed.

  2. Creates and adds a new row to model. The created row object has a fluent api.

  3. We set a value for the "First name" column. The ordering does not matter here!

  4. Sets a column called "Hobbies". Here we use the delimiter inside the data which lead to auto escaping of the value later

Output will be:

First name;Last name;Hobbies
Albert;Tregnaghi;"Software development; Electronic games, Book reading"(1)
  1. The last column which contains the delimiter char inside, is automatically escaped with character "

Example 2

Parse a CSV string without headlines to model and fetch content by API
String csv = "Hello;World";

CSVParser parser = new CSVParser();
CSVModel model = parser.parse(csv,false);

// Because no headlines are defined/fetched, auto column names are created
// The name pattern is : "col${columnIndex}"
String value1 = model.getCellValue("col0", 0);
String value2 = model.getCellValue("col1", 0);
System.out.println(value1+" "+value2);

Output will be:

Hello World

Example 3

Parse a CSV string with headlines to model and fetch content by API
String csv = "Word1;Word2\nHello;World\nOther;Line";

CSVParser parser = new CSVParser();
CSVModel model = parser.parse(csv,true);

// We use now the headlines defined inside CSV file:
String value1 = model.getCellValue("Word1", 0);
String value2 = model.getCellValue("Word2", 0);
System.out.println(value1+" "+value2);

Output will be:

Hello World

Contribution guide

Contributions are welcome.

Git setup

git clone [email protected]:de-jcup/commons-csv.git

Wanted git setup:

git config branch.autosetuprebase always
git config branch.master.rebase true
git config push.default current
git config core.autocrlf input
git config color.ui auto
git config --add remote.origin.fetch +refs/tags/*:refs/tags/*

Build

./gradlew build

commons-csv's People

Contributors

de-jcup avatar

Watchers

 avatar  avatar

commons-csv's Issues

Rename the library because too similar to apache commons-csv

Situation

The idea behind "de.jcup.commons" was based on idea of the Apache commons - with the focus on common things that don’t exist there.

Unfortunately, I only now saw that there is already a similar library from Apache under the exact same name "commons-csv" (see https://commons.apache.org/proper/commons-csv/).

This was not intentional or deliberate. I will continue to use my library, but as I said, there is also a (probably good) alternative from Apache itself.

Wanted

It shall be clear that this library is not a fork for apache commons-csv but another library which is able to handle CSV data and that this library has no dependencies and is small/tiny.

Solution

  • New name: TBD

  • Rename the repository

  • Create a new repositroy with old name, link the new repository (so old

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.