在Kotlin中逐行读取CSV

我正在编写一个简单的导入应用程序,需要读取一个CSV文件,在网格中显示结果,并在另一个网格中显示CSV文件的损坏行。

有没有内置的lib或任何简单pythonic的方式吗?

我在android上做。

使用opencsv 。

这就像阅读CSV文件的魅力一样。

至于记录损坏的线路,你可以使用这个逻辑。

while(input.hasNextLine()) { try { //execute commands by reading them using input.nextLine() } catch (ex: UserDefinedException) { //catch/log the exceptions you're throwing // log the corrupted line the continue to next iteration } } 

希望这可以帮助。

我用net.sourceforge.javacsv和我的Kotlin代码解析CSV文件。 这是一个“java”库,但是在kotlin中,它就像是直接与它一起工作

 val reader = CsvReader("/path/to/file.csv").apply { trimWhitespace = true skipEmptyRecords = true readHeaders() } while (reader.readRecord()) { // do whatever }