All Projects → doyaaaaaken → Kotlin Csv

doyaaaaaken / Kotlin Csv

Licence: apache-2.0
Pure Kotlin CSV Reader/Writer

Programming Languages

kotlin
9241 projects
dsl
153 projects

Labels

Projects that are alternatives of or similar to Kotlin Csv

Csv.jl
Utility library for working with CSV and other delimited files in the Julia programming language
Stars: ✭ 208 (-12.61%)
Mutual labels:  csv
Codablecsv
Read and write CSV files row-by-row or through Swift's Codable interface.
Stars: ✭ 214 (-10.08%)
Mutual labels:  csv
Django Data Wizard
🧙⚙️ Import structured data (e.g. Excel, CSV, XML, JSON) into one or more Django models via an interactive web-based wizard
Stars: ✭ 227 (-4.62%)
Mutual labels:  csv
Jsonexport
{} → 📄 it's easy to convert JSON to CSV
Stars: ✭ 208 (-12.61%)
Mutual labels:  csv
Csview
📠 A high performance csv viewer with cjk/emoji support.
Stars: ✭ 208 (-12.61%)
Mutual labels:  csv
Docto
Simple command line utility for converting .doc & .xls files to any supported format such as Text, RTF, CSV or PDF
Stars: ✭ 220 (-7.56%)
Mutual labels:  csv
Chronicle Wire
A Java Serialisation Library that supports multiple formats
Stars: ✭ 204 (-14.29%)
Mutual labels:  csv
Test Lists
URL testing lists intended for discovering website censorship
Stars: ✭ 236 (-0.84%)
Mutual labels:  csv
Json To Csv
Nested JSON to CSV Converter
Stars: ✭ 216 (-9.24%)
Mutual labels:  csv
Gotenberg
A Docker-powered stateless API for PDF files.
Stars: ✭ 3,272 (+1274.79%)
Mutual labels:  csv
Json 2 Csv
Convert JSON to CSV *or* CSV to JSON!
Stars: ✭ 210 (-11.76%)
Mutual labels:  csv
Octosql
OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases and file formats using SQL.
Stars: ✭ 2,579 (+983.61%)
Mutual labels:  csv
Userline
Query and report user logons relations from MS Windows Security Events
Stars: ✭ 221 (-7.14%)
Mutual labels:  csv
Python Benedict
dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. 📘
Stars: ✭ 204 (-14.29%)
Mutual labels:  csv
Algeria Cities
The list of all Algerian provinces and cities according to the official division in different formats: csv, xlsx, php, json, etc.
Stars: ✭ 232 (-2.52%)
Mutual labels:  csv
Unbescape
Advanced yet easy to use escaping library for Java
Stars: ✭ 207 (-13.03%)
Mutual labels:  csv
Babelish
Chaotically confused, like Babel
Stars: ✭ 217 (-8.82%)
Mutual labels:  csv
Mapshaper
Tools for editing Shapefile, GeoJSON, TopoJSON and CSV files
Stars: ✭ 2,813 (+1081.93%)
Mutual labels:  csv
Semantic Csv
Higher level tools for working with CSV data and files
Stars: ✭ 232 (-2.52%)
Mutual labels:  csv
Portphp
Data import/export framework for PHP
Stars: ✭ 225 (-5.46%)
Mutual labels:  csv

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.15.1")

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

maven:

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

kscript

@file:DependsOn("com.github.doyaaaaaken:kotlin-csv-jvm:0.15.1")

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()
}

Write in a Suspending Function

val rows = listOf(listOf("a", "b", "c"), listOf("d", "e", "f")).asSequence()
csvWriter().openAsync(testFileName) {
    delay(100) //other suspending task
    rows.asFlow().collect {
       delay(100) // other suspending task
        writeRow(it)
    }
}

Read in a Suspending Function

csvReader().openAsync("test.csv") {
    val container = mutalbeListOf<List<String>>()
    delay(100) //other suspending task
    readAllAsSequence().asFlow().collect { row ->
       delay(100) // other suspending task
       container.add(row) 
    }
}

Note: openAsync can be and only be accessed through a coroutine or another suspending function

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))
}

long-running write (manual control for file close)

If you want to close file writer manually for performance reason (i.e. streaming scenario), you can use openAndGetRawWriter and get raw CsvFileWriter.
DO NOT forget to call close method manually.

val row1 = listOf("a", "b", "c")
@OptIn(KotlinCsvExperimental::class)
val writer = csvWriter().openAndGetRawWriter("test.csv") 
writer.writeRow(row1)
writer.close()

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 = '\''
    }
}
Option 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.
- NON_NUMERIC: Quote non-numeric fields. (ex. 1,"a",2.3)

Links

Documents

Libraries which use kotlin-csv

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].