如何在csvformat中避免逗号前加反斜杠

tag5nh1u  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(489)

我正在使用java中的csvformat创建一个csv文件,我在头文件和值中面临的问题是,每当字符串很长并且有一个逗号时,api总是在逗号之前插入\。因此,标头的格式不正确,csv文件中的值将取下一个单元格作为标题。我正在发布我所做的代码

try (CSVPrinter csvPrinter = new CSVPrinter(out,
            CSVFormat.DEFAULT.withHeader("\""+SampleEnum.MY_NAME.getHeader()+"\"", "\""+SampleEnum.MY_TITLE.getHeader()+"\"",
                    "\""+SampleEnum.MY_ID.getHeader()+"\"", "\""+SampleEnum.MY_NUMBER.getHeader()+"\"", "\""+SampleEnum.MY_EXTERNAL_KEY.getHeader()+"\"",
                    "\""+SampleEnum.DATE.getHeader()+"\"","\""+SampleEnum.MY_ACTION.getHeader()+"\"",
                    "\"\"\""+SampleEnum.MY__DEFI.getHeader()+"\"\"\"", SampleEnum.MY_ACTION.getHeader(),
                    SampleEnum.CCHK.getHeader(), SampleEnum.DISTANCE_FROM_LOCATION.getHeader(),
                    SampleEnum.TCOE.getHeader(), SampleEnum.HGTR.getHeader(),SampleEnum._BLANK.getHeader(),
                    SampleEnum.LOCATION_MAP.getHeader(), SampleEnum.SUBMISSION_ID.getHeader())                      
                    .withDelimiter(',').withEscape('\\').withQuote('"').withTrim().withQuoteMode(QuoteMode.MINIMAL)
    )) {
        sampleModel.forEach(sf -> {
            try {
                csvPrinter.printRecord(sf.getMyName(),
                        sf.getMyTitle(),
                        sf.getMyID(),
                        sf.getMyNo(),

所以现在的问题是我得到的输出是这样的

"\"Name:\"","\"Title\"","\"ID #:\"","\"Store #:\"","\"Store #: External Key\"","\"Date:\"","\"\"\"It's performance  issue in detail to include dates,times, circumstances, etc.\"\"\""

我在每个逗号前都有\个字符,当这个值出现时,文本的下一部分将移到下一个单元格。
我需要的输出是

"Name:","Title:","Employee ID #:","Store #:","Store #: CurrierKey","Date:","Stage of Disciplinary Action:","""Describe your view about the company, times, circumstances, etc.""",

我正在努力https://commons.apache.org/proper/commons-csv/jacoco/org.apache.commons.csv/csvformat.java.html 此链接,但我无法理解修复。请帮忙。

w8ntj3qf

w8ntj3qf1#

这是因为您正在使用 QuoteMode.NONE 其中包含以下javadoc:
从不引用字段。当分隔符出现在数据中时,打印机会在其前面加上转义字符。如果未设置转义字符,格式验证将引发异常。
你可以用 QuoteMode.MINIMAL 仅引用包含特殊字符的字段(例如,字段分隔符、引号字符或行分隔符字符串的字符)。
我建议你使用 CSVFormat.DEFAULT 如果无法使用其他格式之一,请自行配置所有内容。检查反斜杠是否正确( \ )是您的用例的正确转义字符。通常是双引号( " ). 此外,您可能希望删除标题定义中的所有双引号,因为它们会根据您的配置自动添加(如果需要)。

StringBuilder out = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(out,
        CSVFormat.DEFAULT
                .withHeader("AAAA", "BB\"BB", "CC,CC", "DD'DD")
                .withDelimiter(',')
                .withEscape('\\') // <- maybe you want '"' instead
                .withQuote('"').withRecordSeparator('\n').withTrim()
                .withQuoteMode(QuoteMode.MINIMAL)
)) {
    csvPrinter.printRecord("WWWW", "XX\"XX", "YY,YY", "ZZ'ZZ");
}
System.out.println(out);
AAAA,"BB\"BB","CC,CC",DD'DD
WWWW,"XX\"XX","YY,YY",ZZ'ZZ

编辑之后,似乎希望所有字段都用双引号作为转义字符。因此,您可以使用 QuoteMode.ALL.withEscape('"') 这样地:

StringBuilder out = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(out,
        CSVFormat.DEFAULT
                .withHeader("AAAA", "BB\"BB", "CC,CC", "\"DD\"", "1")
                .withDelimiter(',')
                .withEscape('"')
                .withQuote('"').withRecordSeparator('\n').withTrim()
                .withQuoteMode(QuoteMode.ALL)
)) {
    csvPrinter.printRecord("WWWW", "XX\"XX", "YY,YY", "\"DD\"", "2");
}
System.out.println(out);
"AAAA","BB""BB","CC,CC","""DD""","1"
"WWWW","XX""XX","YY,YY","""DD""","2"

在注解中,您声明只需要在需要时使用双引号,并且只需要在一个字段中使用三引号。然后,你可以使用 QuoteMode.MINIMAL.withEscape('"') 正如第一个例子中所建议的。当您将该字段的输入用双引号括起来时,会生成三引号(一次是因为有一个特殊字符,需要引用该字段,第二次是因为添加了显式 " 第三个是为了逃避你的直截了当的引用)。

StringBuilder out = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(out,
        CSVFormat.DEFAULT
                .withHeader("AAAA", "BB\"BB", "CC,CC", "\"DD\"", "1")
                .withDelimiter(',')
                .withEscape('"')
                .withQuote('"').withRecordSeparator('\n').withTrim()
                .withQuoteMode(QuoteMode.MINIMAL)
)) {
    csvPrinter.printRecord("WWWW", "XX\"XX", "YY,YY", "\"DD\"", "2");
}
System.out.println(out);
AAAA,"BB""BB","CC,CC","""DD""",1
WWWW,"XX""XX","YY,YY","""DD""",2

根据聊天记录,当标题有引号和没有引号时,您需要完全控制。不存在以下组合: QuoteMode 和转义字符,将给出所需的结果。因此,我建议您手动构造标题:

StringBuilder out = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(out,
        CSVFormat.DEFAULT
                .withDelimiter(',').withEscape('"')
                .withQuote('"').withRecordSeparator('\n').withTrim()
                .withQuoteMode(QuoteMode.MINIMAL))
) {
    out.append(String.join(",", "\"AAAA\"", "\"BBBB\"", "\"CC,CC\"", "\"\"\"DD\"\"\"", "1"));
    out.append("\n");
    csvPrinter.printRecord("WWWW", "XX\"XX", "YY,YY", "\"DD\"", "2");
}
System.out.println(out);
"AAAA","BBBB","CC,CC","""DD""",1
WWWW,"XX""XX","YY,YY","""DD""",2

相关问题