kotlin-csv alternatives and similar libraries
Based on the "Misc" category.
Alternatively, view kotlin-csv alternatives based on common mentions on social networks and blogs.
-
jclasslib
jclasslib bytecode editor is a tool that visualizes all aspects of compiled Java class files and the contained bytecode. -
kotlin-logging
Lightweight Multiplatform logging framework for Kotlin. A convenient and performant logging facade. -
kotlin-telegram-bot
๐ค A wrapper for the Telegram Bot API written in Kotlin -
kotlinx.atomicfu
The idiomatic way to use atomic operations in Kotlin -
tinylog
tinylog is a lightweight logging framework for Java, Kotlin, Scala, and Android -
lingua
The most accurate natural language detection library for Java and the JVM, suitable for long and short text alike -
Kotlift
Kotlift is the first source-to-source language transpiler from Kotlin to Swift -
kotlinx.reflect.lite
Lightweight library allowing to introspect basic stuff about Kotlin symbols -
Humanizer.jvm
Humanizer.jvm meets all your jvm needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities. -
actions-on-google-kotlin
Unofficial Actions on Google SDK for Kotlin and Java -
klutter
A mix of random small libraries for Kotlin, the smallest reside here until big enough for their own repository. -
solr-undertow
Solr / SolrCloud running in high performance server - tiny, fast startup, simple to configure, easy deployment without an application server. -
kassava
This library provides some useful kotlin extension functions for implementing toString(), hashCode() and equals() without all of the boilerplate. -
SimpleDNN
SimpleDNN is a machine learning lightweight open-source library written in Kotlin designed to support relevant neural network architectures in natural language processing tasks -
units-of-measure
Type-safe dimensional analysis and unit conversion in Kotlin. -
TLSLibrary
Simple TlsLibrary written in Kotlin - Provides DSL for creating TLS connections -
kotlin-futures
A collections of extension functions to make the JVM Future, CompletableFuture, ListenableFuture API more functional and Kotlin like. -
scientist
A kotlin library for refactoring code. Port of GitHub's scientist. -
kasechange
๐ซ๐๐ข๐ ฟ Multiplatform Kotlin library to convert strings between various case formats including Camel Case, Snake Case, Pascal Case and Kebab Case -
kjob
A lightweight coroutine based persistent job/cron scheduler written in Kotlin -
PrimeCalendar
PrimeCalendar provides all of the java.util.Calendar functionalities for Persian, Hijri, and ... dates. It is also possible to convert dates to each other. -
kotlin-pluralizer
:sunny: Kotlin extension to pluralize and singularize strings -
kformula
Mathematical expression engine written in Kotlin, running on JVM. -
kase-format
Multiplatform kotlin string case conversion and detection library.
Appwrite - The Open Source Firebase alternative introduces iOS support
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of kotlin-csv or a related project?
README
kotlin-csv
Pure Kotlin CSV Reader/Writer.
Design goals
1. Simple interface
- easy to setup
- use DSL so easy to read
2. Automatic handling of I/O
- in 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
- kotlin multiplatform project
Usage
Download
Gradle
//gradle kotlin DSL
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.7.0") //for JVM platform
implementation("com.github.doyaaaaaken:kotlin-csv-js:1.7.0") //for Kotlin JS platform
//gradle groovy DSL
implementation 'com.github.doyaaaaaken:kotlin-csv-jvm:1.7.0' //for JVM platform
implementation 'com.github.doyaaaaaken:kotlin-csv-js:1.7.0' //for Kotlin JS platform
Maven
<dependency>
<groupId>com.github.doyaaaaaken</groupId>
<artifactId>kotlin-csv-jvm</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.github.doyaaaaaken</groupId>
<artifactId>kotlin-csv-js</artifactId>
<version>1.7.0</version>
</dependency>
@file:DependsOn("com.github.doyaaaaaken:kotlin-csv-jvm:1.7.0") //for JVM platform
@file:DependsOn("com.github.doyaaaaaken:kotlin-csv-js:1.7.0")
//for Kotlin JS platform
Examples
CSV Read examples
Simple case
You can read csv file from String
, java.io.File
or java.io.InputStream
object.
No need to do any I/O handling. (No need to call use
, close
and flush
method.)
// 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 only be called within the open
lambda block.
The input stream is closed after the open
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()
}
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 = '\\'
}
Option | default value | description |
---|---|---|
logger | no-op | Logger instance for logging debug information at runtime. |
charset | UTF-8 |
Charset encoding. The value must be supported by java.nio.charset.Charset. |
quoteChar | " |
Character used to quote fields. |
delimiter | , |
Character used as delimiter between each field.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](src/commonMain/kotlin/com/github/doyaaaaaken/kotlincsv/dsl/context/CsvReaderContext.kt). |
skipEmptyLine | false |
Whether to skip or error out on empty lines. |
autoRenameDuplicateHeaders | false |
Whether to auto rename duplicate headers or throw an exception. |
false |
Deprecated. Replace with appropriate values in excessFieldsRowBehaviour and insufficientFieldsRowBehaviour , e.g. both set to IGNORE . ignoreExcessCols is true, only rows with less than the expected number of columns will be skipped. |
|
excessFieldsRowBehaviour | ERROR |
Behaviour to use when a row has more fields (columns) than expected. ERROR (default), IGNORE (skip the row) or TRIM (remove the excess fields at the end of the row to match the expected number of fields). |
insufficientFieldsRowBehaviour | ERROR |
Behaviour to use when a row has fewer fields (columns) than expected. ERROR (default), IGNORE (skip the row). |
CSV Write examples
Simple case
You can start writing csv in one line, no need to do any I/O handling (No need 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 into OutpusStream.
csvWriter().writeAll(rows, File("test.csv").outputStream())
You can also write a csv file line by line by open
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))
}
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)
}
}
Write as String
val rows = listOf(listOf("a", "b", "c"), listOf("d", "e", "f"))
val csvString: String = csvWriter().writeAllAsString(rows) //a,b,c\r\nd,e,f\r\n
long-running write (manual control for file close)
If you want to close a file writer manually for performance reasons (e.g. streaming scenario), you can
use openAndGetRawWriter
and get a raw CsvFileWriter
.
DO NOT forget to close
the writer!
val row1 = listOf("a", "b", "c")
@OptIn(KotlinCsvExperimental::class)
val writer = csvWriter().openAndGetRawWriter("test.csv")
writer.writeRow(row1)
writer.close()
Customize
When you create a 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
- kotlin-grass: Csv File to Kotlin Data Class Parser.
Miscellaneous
๐ค Contributing
Contributions, issues and feature requests are welcome!
If you have questions, ask away 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 licensed under [Apache 2.0](LICENSE).
This project is inspired โค๏ธ by scala-csv
This README was generated with โค๏ธ by readme-md-generator
*Note that all licence references and agreements mentioned in the kotlin-csv README section above
are relevant to that project's source code only.