parseCsvWithHeaders
Parses the CSV data with headers from a flow of strings and emits each row as a map where the keys are the headers and the values are the corresponding row values.
Return
A flow of maps, each representing a row of parsed CSV values with headers as keys.
Example usage:
val csvFlow = flowOf("Name;Age", "Alice;30", "Bob;25")
csvFlow
.parseCsvWithHeaders()
.collect { row -> println(row) }
Content copied to clipboard
Parameters
delimiter
The delimiter to use for separating values in the CSV. Defaults to ";".
fun <T> Flow<String>.parseCsvWithHeaders(delimiter: String = ";", f: (Map<String, String>) -> T): Flow<T>
Parses the CSV data with headers from a flow of strings and emits each row as a custom object, converted using the provided function.
Return
A flow of custom objects, each representing a row of parsed CSV values with headers as keys.
Example usage:
data class Person(val name: String, val age: Int)
val csvFlow = flowOf("Name;Age", "Alice;30", "Bob;25")
csvFlow
.parseCsvWithHeaders { row -> Person(checkNotNull(row["Name"]), checkNotNull(row["Age"]?.toInt())) }
.collect { person -> println(person) }
Content copied to clipboard
Parameters
delimiter
The delimiter to use for separating values in the CSV. Defaults to ";".
f
The function to convert the parsed row (map of header-value pairs) into a custom object.