I'm writing a simple import application and need to read a CSV file, show result in a grid and show corrupted lines of the CSV file in another grid.
Is there any built-in lib for it or any easy pythonic-like way?
I'm doing it on android.
To make it more readable we should create it with column names so that we can call the values by their column name. We have to create headers to call the values with their name, We can set the header while creating the CSVParser. Complete program for reading CSV file with Column Names in kotlin.
1) Select "Project" from the top of the vertical toolbar to open the project "tool window" 2) From the drop-down menu at the top of the "tool window", select "Android" 3) Right-click on "App" and select "New" then -> "Folder" (the one with the green Android icon beside it) then -> "Assets Folder" 4) Right-click on the ...
If you already have Microsoft Excel installed, just double-click a CSV file to open it in Excel. After double-clicking the file, you may see a prompt asking which program you want to open it with. Select Microsoft Excel. If you are already in Microsoft Excel, you can choose File > Open and select the CSV file.
[Edit October 2019] A couple of months after I wrote this answer, Koyama Kenta wrote a Kotlin targeted library which can be found at https://github.com/doyaaaaaken/kotlin-csv and which looks much better to me than opencsv.
Example usage: (for more info see the github page mentioned)
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader fun main() { csvReader().open("src/main/resources/test.csv") { readAllAsSequence().forEach { row -> //Do something println(row) //[a, b, c] } } }
For a complete minimal project with this example, see https://github.com/PHPirates/kotlin-csv-reader-example
Old answer using opencsv:
As suggested, it is convenient to use opencsv. Here is a somewhat minimal example:
// You can of course remove the .withCSVParser part if you use the default separator instead of ; val csvReader = CSVReaderBuilder(FileReader("filename.csv")) .withCSVParser(CSVParserBuilder().withSeparator(';').build()) .build() // Maybe do something with the header if there is one val header = csvReader.readNext() // Read the rest var line: Array<String>? = csvReader.readNext() while (line != null) { // Do something with the data println(line[0]) line = csvReader.readNext() }
As seen in the docs when you do not need to process every line separately you can get the result in the form of a Map:
import com.opencsv.CSVReaderHeaderAware import java.io.FileReader fun main() { val reader = CSVReaderHeaderAware(FileReader("test.csv")) val resultList = mutableListOf<Map<String, String>>() var line = reader.readMap() while (line != null) { resultList.add(line) line = reader.readMap() } println(resultList) // Line 2, by column name println(resultList[1]["my column name"]) }
Dependency for Gradle: compile 'com.opencsv:opencsv:4.6'
or for Gradle Kotlin DSL: compile("com.opencsv:opencsv:4.6")
(as always, check for latest version in docs).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With