Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache common CSVParser/CSVRecord to return null for empty fields

Tags:

java

csv

all

I have a question for Apache common CSVParser/CSVRecord. Take a look at the CSV file below:

Header1,Header2,Header3
"",,"L1C3"

CSVParser/CSVRecord is returning "" for the first two columns. In my case I want to distinguish empty string("") and null values. Is there a configuration I could set to let CSVParser to return null for the second column?

Thank you.

like image 988
Cary Li Avatar asked Jan 12 '16 01:01

Cary Li


1 Answers

I've used this format :

CSVFormat.RFC4180.withFirstRecordAsHeader()
   .withIgnoreSurroundingSpaces()
   .withNullString("")

Where the 2 configurations:

  1. ignore space - which trims any value on both sides, if its all space, it will be trimmed to empty space
  2. null string - which treats the empty spaces as null

Here's a sample usage:

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import org.junit.Test;

public class CsvParseTest {

    @Test
    public void testParseWillTrimAndConvertToNull() throws Exception {
        String CSV_HEADER = "Name,MobileNo,Location";
        String CSV_ROW_1 = "abc,   ,australia"; // MobileNo is 3 whitespaces
        CSVParser parse = CSVFormat.RFC4180.withFirstRecordAsHeader().withIgnoreSurroundingSpaces().withNullString("")
                .parse(new BufferedReader(new StringReader(CSV_HEADER + "\n" + CSV_ROW_1)));

        CsvRecord rec = parse.getRecords().get(0);
        assertEquals("abc", rec.get("Name"));
        assertNull(rec.get("MobileNo"));
        assertEquals("australia", rec.get("Location"));
    }
}
like image 136
Carlos Jaime C. De Leon Avatar answered Oct 29 '22 17:10

Carlos Jaime C. De Leon