在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):doyaaaaaken/kotlin-csv开源软件地址(OpenSource Url):https://github.com/doyaaaaaken/kotlin-csv开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):kotlin-csvPure Kotlin CSV Reader/Writer. Design goals1. Simple interface
2. Automatic handling of I/O
3. Multiplatform
UsageDownloadGradle//gradle kotlin DSL
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.4.0") //for JVM platform
implementation("com.github.doyaaaaaken:kotlin-csv-js:1.4.0") //for Kotlin JS platform
//gradle groovy DSL
implementation 'com.github.doyaaaaaken:kotlin-csv-jvm:1.4.0' //for JVM platform
implementation 'com.github.doyaaaaaken:kotlin-csv-js:1.4.0' //for Kotlin JS platform Maven
@file:DependsOn("com.github.doyaaaaaken:kotlin-csv-jvm:1.4.0") //for JVM platform
@file:DependsOn("com.github.doyaaaaaken:kotlin-csv-js:1.4.0")
//for Kotlin JS platform ExamplesCSV Read examplesSimple caseYou can read csv file from // 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 headerval 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 |
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. |
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). |
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))
}
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)
}
}
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()
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) |
Documents
Libraries which use kotlin-csv
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论