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

CSVFormat.format trims last delimiter if the delimiter is a white space

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.4
    • Fix Version/s: 1.9.0
    • Component/s: None
    • Labels:
      None
    • Environment:

      Any.

      Description

      The function CSVFormat.format() trims off the last delimiter if the delimiter is a white space character and the value in the last column is null.

          public String format(Object... values) {
              StringWriter out = new StringWriter();
      
              try {
                  (new CSVPrinter(out, this)).printRecord(values);
                  return out.toString().trim();
              } catch (IOException var4) {
                  throw new IllegalStateException(var4);
              }
          }
      

      For example, consider the following records (\t = tab)-

      ID\tName\tCountry\tAge
      1\tJohn Doe\tUSA\t20
      2\tJane Doe\tUSA\t

      The CSVFormat.format() on both the rows returns the following -

      ID\tName\tCountry\tAge
      1\tJohn Doe\tUSA\t20
      2\tJane Doe\tUSA

      Note that there is a missing delimiter for the last column in the second record.

      This usually causes schema mismatch when we try and read back the CSV file as there is one less column in all the rows which have a null at the end.

      I feel the trim() is unnecessary while returning out.toString()

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                alpeshkulkarni Alpesh Kulkarni
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1h
                  1h