Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse CSV to Kotlin List

Tags:

kotlin

I'm a bit confused. I have this csv line with double quote("") as grouped strings, and want to convert to Kotlin List. However it produces a single array with size 1. I want to be able to get the group, assumes at position 2.

val s = "John Doe, 13, \"Subject 1, Subject 2, Subject 3\""
var list: List<String> = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)")
Log.d("size:", list.size.toString() + " - subjects:" + list[2])

This gives me error

java.lang.IndexOutOfBoundsException: Index: 2, Size: 1
like image 488
luca ditrimma Avatar asked Sep 18 '25 04:09

luca ditrimma


2 Answers

The given regex just works fine. Currently you try to split the string s at the raw regex as delimeter, which does not exists in s. Simply add .toRegex() to the regex.

val s = "John Doe, 13, \"Subject 1, Subject 2, Subject 3\""
var list: List<String> = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)".toRegex())
Log.d("size:", list.size.toString() + " - subjects:" + list[2])
like image 101
pixix4 Avatar answered Sep 21 '25 02:09

pixix4


I'd recommend not doing the parsing yourself, but using an existing library.  (For example, I found Apache Commons CSV easy to use from Kotlin.)

Although writing parsing code can be fun, and CSV seems simple enough, it has enough complications and variations that unless you created it yourself, you're likely to miss some cases.  (Not just escaped quotes, but other escaped characters, nested quotes, fields which include newlines, comment lines…  And my favourite gotcha: MS Excel uses the machine's list separator, which can be semicolon or another character instead of comma to separate fields!)

Trust me, I've been there…

like image 24
gidds Avatar answered Sep 21 '25 02:09

gidds