Apache Commons CSV:用逗号读取值

Apache Commons CSV : Read Values with comma

我正在将 CSV 文件转换为 Java Bean。我需要在包含在 """.

中的值中保留逗号

这是我的代码。

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;

}

示例 CSV 行:

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

我的 DTO

private String precintCode;

private String regionName;

private String provinceName;

private String municipalityName;

private String districtName;

private String votingCenter;

private String precint;

我的预期输出应该是

precintCode = "7080001"

regionName = "XI"

provinceName = "ProvinceX"

municipalityName = "TownX"

districtName = "BRGX"

votingCenter = "SCHOOL, BRGX"

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

然而实际输出是这样的

precintCode = "7080001"

regionName = "XI"

provinceName = "ProvinceX"

municipalityName = "TownX"

districtName = "BRGX"

votingCenter = ""SCHOOL"

precint = " , BRGX,"0054A"

您是否尝试过使用 CSVFormat.DEFAULT constant?-- it's for CSV files adhering to RFC 4180.

此处需要 withIgnoreSurroundingSpaces() 选项。所有其他设置可以保留 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();
    }

输出为

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

我能够使用库中的 withQuote 函数来完成。

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

以下方法对我有用:

CSVFormat.EXCEL.withQuote('"')