Coder Social home page Coder Social logo

jul1u5 / kotlin-csv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from doyaaaaaken/kotlin-csv

0.0 1.0 0.0 296 KB

Pure Kotlin CSV Reader/Writer

Home Page: https://kenta-koyama-biz.gitbook.io/kotlin-csv/

License: Apache License 2.0

Kotlin 100.00%

kotlin-csv's Introduction

Welcome to kotlin-csv ๐Ÿ‘‹

Version License: Apache License 2.0 CircleCI codecov CodeFactor

Pure Kotlin CSV Reader/Writer

Principals

1. Simple interface

  • easy to setup
  • use DSL so easy to read

2. No need to be aware of file close

  • on Java, we always need to close file. but it's boilerplate code and not friendly for non-JVM user.
  • provide interfaces which automatically close file without being aware.

3. Multiplatform (Planned in #15)

  • kotlin multiplatform project

Usage

Download

gradle DSL:

//gradle kotlin DSL
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:0.10.4")

//gradle groovy DSL
implementation 'com.github.doyaaaaaken:kotlin-csv-jvm:0.10.4'

maven:

<dependency>
  <groupId>com.github.doyaaaaaken</groupId>
  <artifactId>kotlin-csv-jvm</artifactId>
  <version>0.10.4</version>
</dependency>

Examples

CSV Read examples

Simple case

You can read csv file from String, java.io.File or java.io.InputStream object.

// read from `String`
val csvData: String = "a,b,c\nd,e,f"
val rows: List<List<String>> = csvReader().readAll(csvData)

// read from `java.io.File`
val file: File = File("test.csv")
val rows: List<List<String>> = csvReader().readAll(file)

Read with header

val csvData: String = "a,b,c\nd,e,f"
val rows: List<Map<String, String>> = csvReader().readAllWithHeader(csvData)
println(rows) //[{a=d, b=e, c=f}]

Read as Sequence

Sequence type allows to execute lazily.
It starts to process each rows before reading all row data.

See detail about Sequence type on Kotlin official document.

csvReader().open("test1.csv") {
    readAllAsSequence().forEach { row: List<String> ->
        //Do something
        println(row) //[a, b, c]
    }
}

csvReader().open("test2.csv") {
    readAllWithHeaderAsSequence().forEach { row: Map<String, String> ->
        //Do something
        println(row) //{id=1, name=doyaaaaaken}
    }
}

NOTE:readAllAsSequence and readAllWithHeaderAsSequence methods can be only called inside open method lambda block. Because, input stream is closed outside open method lambda block.

Read line by line

If you want to handle line-by-line, you can do it by using open method.
Use open method and then use readNext method inside nested block to read row.

csvReader().open("test.csv") {
    readNext()
}

Customize

When you create CsvReader, you can choose read options.

// this is tsv reader's option
val tsvReader = csvReader {
    charset = "ISO_8859_1"
    quoteChar = '"'
    delimiter = '\t'
    escapeChar = '\\'
}
Opton default value description
charset UTF-8 Charset encoding. The value must be supported by java.nio.charset.Charset.
quoteChar " Character used as quote between each fields.
delimiter , Character used as delimiter between each fields.
Use "\t" if reading TSV file.
escapeChar " Character to escape quote inside field string.
Normally, you don't have to change this option.
See detail comment on ICsvReaderContext.
skipEmptyLine false If empty line is found, skip it or not (=throw an exception).
skipMissMatchedRow false If a invalid row which has different number of fields from other rows is found, skip it or not (=throw an exception).

CSV Write examples

Simple case

You can write csv simply, only one line. No need to call other methods.
Also, You don't have to call use, close and flush method.

val rows = listOf(listOf("a", "b", "c"), listOf("d", "e", "f"))
csvWriter().writeAll(rows, "test.csv")

// if you'd append data on the tail of the file, assign `append = true`.
csvWriter().writeAll(rows, "test.csv", append = true)

You can also write csv file per each line.
Also, You don't have to call use, close and flush method.

val row1 = listOf("a", "b", "c")
val row2 = listOf("d", "e", "f")
csvWriter().open("test.csv") { 
    writeRow(row1)
    writeRow(row2)
    writeRow("g", "h", "i")
    writeRows(listOf(row1, row2))
}

Customize

When you create CsvWriter, you can choose write options.

val writer = csvWriter {
    charset = "ISO_8859_1"
    delimiter = '\t'
    nullCode = "NULL"
    lineTerminator = "\n"
    outputLastLineTerminator = true
    quote {
        mode = WriteQuoteMode.ALL
        char = '\''
    }
}
Opton default value description
charset UTF-8 Charset encoding. The value must be supported by java.nio.charset.Charset.
delimiter , Character used as delimiter between each fields.
Use "\t" if reading TSV file.
nullCode (empty string) Character used when a written field is null value.
lineTerminator \r\n Character used as line terminator.
outputLastLineTerminator true Output line break at the end of file or not.
quote.char " Character to quote each fields.
quote.mode CANONICAL Quote mode.
- CANONICAL: Not quote normally, but quote special characters (quoteChar, delimiter, line feed). This is the specification of CSV.
- ALL: Quote all fields.

Miscellaneous

๐Ÿค Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.
If you have question, feel free to ask in Kotlin slack's kotlin-csv room.

๐Ÿ’ป Development

$ git clone [email protected]:doyaaaaaken/kotlin-csv.git
$ cd kotlin-csv
$ ./gradlew check

Show your support

Give a โญ๏ธ if this project helped you!

๐Ÿ“ License

Copyright ยฉ 2019 doyaaaaaken.
This project is Apache License 2.0 licensed.


This project is inspired โค๏ธ by scala-csv

This README was generated with โค๏ธ by readme-md-generator

kotlin-csv's People

Contributors

doyaaaaaken avatar blackmo18 avatar

Watchers

 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.