Aaron
Aaron

Reputation: 2755

Apache Commons CSV : Read Values with comma

I am converting CSV files to a Java Bean. I need to maintain the comma inside a value which is enclosed in "".

Here is my code.

public static PPRCV convertContestToObj(String fileName) throws IOException {

    PPRCV pprcvHandler = PPRCVFactory.getPPRCVTable(fileName);

    CSVFormat csvFileFormat = CSVFormat.DEFAULT.newFormat(',').withEscape('"');

    List<PPRCV> pprcvs = new ArrayList<>();
    FileReader fileReader = new FileReader(fileName);

    CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat);

    List<CSVRecord> csvRecords = csvFileParser.getRecords();

    for (CSVRecord csvRecord : csvRecords) {
        pprcvs.add(pprcvHandler.populateDynamicDetails(csvRecord));
    }

    return pprcvHandler;

}

Sample CSV line:

7080001, XI, ProvinceX, TownX, BRGX, "SHOOL, BRGX", "0054A,0055A,0055B,0055C"

my DTO

private String precintCode;

private String regionName;

private String provinceName;

private String municipalityName;

private String districtName;

private String votingCenter;

private String precint;

My expected output should be

precintCode = "7080001"

regionName = "XI"

provinceName = "ProvinceX"

municipalityName = "TownX"

districtName = "BRGX"

votingCenter = "SCHOOL, BRGX"

precint = "0054A,0055A,0055B,0055C"

However actual output is this

precintCode = "7080001"

regionName = "XI"

provinceName = "ProvinceX"

municipalityName = "TownX"

districtName = "BRGX"

votingCenter = ""SCHOOL"

precint = " , BRGX,"0054A"

Upvotes: 3

Views: 5768

Answers (4)

cmvf
cmvf

Reputation: 1

The following way worked for me:

CSVFormat.EXCEL.withQuote('"')

Upvotes: 0

Aaron
Aaron

Reputation: 2755

I was able to do it using the withQuote function from the library.

CSVFormat.EXCEL.newFormat(',').withQuote('"')

Upvotes: 1

kwarnke
kwarnke

Reputation: 1514

You need the withIgnoreSurroundingSpaces() optione here. All other settings could be remain DEFAULT.

    final Reader in = new StringReader("7080001, XI, ProvinceX, TownX, BRGX, \"SHOOL, BRGX\", \"0054A,0055A,0055B,0055C\" ");
    final CSVFormat csvFileFormat = CSVFormat.DEFAULT.withIgnoreSurroundingSpaces();

    for (CSVRecord record: csvFileFormat.parse(in)) {
        for (String field: record) {
            System.out.println("\"" + field + "\"");
        }
        System.out.println();
    }

The output is

"7080001"
"XI"
"ProvinceX"
"TownX"
"BRGX"
"SHOOL, BRGX"
"0054A,0055A,0055B,0055C"

Upvotes: 5

errantlinguist
errantlinguist

Reputation: 3818

Have you already tried using the CSVFormat.DEFAULT constant?-- it's for CSV files adhering to RFC 4180.

Upvotes: 0

Related Questions