Uploaded image for project: 'Commons CSV'
  1. Commons CSV
  2. CSV-296

Delimiter followed by Whitespace then by Quotes Failing with setTrim(true)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8
    • 1.9.0
    • Parser
    • None

    Description

      I have my CSVFormat initialized such that withTrim(true) has been set (see attached ZIP file):

      CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader()
              .withIgnoreEmptyLines(true).withTrim(true);

       

      However, a quoted string that begins after a delimiter followed by preceding whitespace is not properly parsed. For example:

      GIVEN_NAME,SURNAME,ADDRESS,PHONE_NUMBER
      "Joe",  "Schmoe","101 Main Street; Las Vegas, NV 89101","702-555-1212"
      "John","Doe",  "201 First Street; Las Vegas, NV 89102", "702-555-1313"
      "Jane","Doe","301 Second Street; Las Vegas, NV 89103","702-555-1414"
      

       

      • Notice the whitespace preceding "Schmoe" on the first record?  This leads to the actual value containing the quotation marks instead of them being stripped off.
      • The whitespace preceding "201 First Street; Las Vegas, NV 89102" on the second record leads to it to being parsed as two values: "201 First Street; Las Vegas and NV 89102".
      • The third record is the only one that parses as expected.

      I believe that this is because the trimming is done after the value is being parsed rather than consuming the whitespace following the delimiter during parsing.   Either that, or the check for a quoted string is occurring before the whitespace is being consumed.

       

      NOTE: I have attached a ZIP file that easily reproduces the problem with the CSV file given above.

      To build the attached project use Apache Maven and then execute using using Java 11:

      > unzip csvfail.zip
      > cd csvfail
      > mvn package
      > java -jar target/csv-fail-1.0-SNAPSHOT.jar

      Attachments

        1. csvfail.zip
          63 kB
          Barry M. Caceres

        Activity

          People

            Unassigned Unassigned
            bcaceres Barry M. Caceres
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: