Giter Site home page Giter Site logo

Comments (5)

StefRe avatar StefRe commented on July 1, 2024 1

Thanks for your reply.
RFC 4180 just describes the file format with empty fields being explicitly allowed:

field = (escaped / non-escaped)
escaped = DQUOTE *(TEXTDATA / COMMA / CR / LF / 2DQUOTE) DQUOTE
non-escaped = *TEXTDATA

However, it doesn't say anything about how to interpret the data, specifically it doesn't mention if an empty field should be an empty string or null.

For my use case (a mock ResultSet backed by csv data) it is essential to differentiate between an empty string "" and null.

OpenCSV ("com.opencsv:opencsv:5.4") provides fine-grained configuration of what the parser should consider null:

EMPTY_SEPARATORS - two sequential separators are null.
EMPTY_QUOTES - two sequential quotes are null
BOTH - both are null
NEITHER - default. Both are considered empty string.

and allows to do exactly what I need:

import com.opencsv.CSVReaderHeaderAware
import com.opencsv.CSVReaderHeaderAwareBuilder
import com.opencsv.enums.CSVReaderNullFieldIndicator

val csv = """
    "empty col","null col"
    "",
""".trimIndent()

fun main() {
    csv.byteInputStream().bufferedReader().use { br ->
        (CSVReaderHeaderAwareBuilder(br)
            .withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS)
            .build() as CSVReaderHeaderAware)
            .use { cr ->
                val csvData = cr.readMap()
                assert(csvData["empty col"] == "")
                assert(csvData["null col"] == null)
            }
    }
}

So I can switch to OpenCSV but it's a bit heavy-weight as it does far more than I actually need so I'd prefer to stick with your lib for Kotlin projects.

from kotlin-csv.

lucasgust avatar lucasgust commented on July 1, 2024 1

I really want this feature...

from kotlin-csv.

doyaaaaaken avatar doyaaaaaken commented on July 1, 2024

@StefRe
Thank you for your feedback!!
I'll think about this on weekend, thanks.

from kotlin-csv.

doyaaaaaken avatar doyaaaaaken commented on July 1, 2024

@StefRe
Thank you for your input.
In your case, you want to regard "" as empty string and `` as null value.

It's maybe possible to have a case like the one you encountered.
However, I looked at the RFC 4180 and considered carefully, then I've felt that your case is a special case in terms of the CSV specification.

I'm very sorry, but I'll wait to deal with it until I get same feedback from others.

from kotlin-csv.

doyaaaaaken avatar doyaaaaaken commented on July 1, 2024

@StefRe
Well, thank you for the good suggestions.
I'm convinced of your opinion.
Especially that configuration of OpenCSV seems to be also applicable for kotlin-csv.
I'd like to consider this topic as a feature request and try to implement it.

from kotlin-csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.