Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read CSV line-by-line in Kotlin

Tags:

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.

like image 574
Marcus Pólo Avatar asked May 19 '17 03:05

Marcus Pólo


People also ask

How do I create a CSV file in Kotlin?

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.

How do I read a kotlin file?

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 ...

How do I view a CSV file?

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.


1 Answers

[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).

like image 81
PHPirate Avatar answered Sep 27 '22 17:09

PHPirate