【问题标题】:how to write a blank column with CSVPrinter of apache.commons.csv如何用 apache.commons.csv 的 CSVPrinter 写一个空白列
【发布时间】:2017-10-31 17:54:05
【问题描述】:

我正在使用 CSVPrinter 来读取和写入 CSV。在编写 CSV 时, 我想写一个空白列,例如“x”,“y”(第二列)。但不幸的是,它写为“x”、“”、“y”。如何使用 CSVPrinter 编写空白列?我使用以下内容来编写它。甚至,我在 CSVPrinter 文档中找不到它。请帮帮我。

printer.print(null) -> "" 打印机.print("") -> ""

谢谢

【问题讨论】:

  • 请发布您正在使用的 CSV 格式。默认输出不同。

标签: java apache csv export-to-csv


【解决方案1】:

您要查找的行为仅在 1.5 版中添加:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.5</version>
    </dependency>

使用此 API,您可以应用 ALL_NON_NULL 引用模式:

    CSVFormat customFormat = CSVFormat.DEFAULT
            .withQuoteMode(QuoteMode.ALL_NON_NULL);

    CSVPrinter printer = new CSVPrinter(System.out, customFormat);
    printer.print("x");
    printer.print("");
    printer.print(null);
    printer.print("y");
    printer.println();

输出:

"x","",,"y"

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 2016-07-17
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    相关资源
    最近更新 更多