gpt4 book ai didi

java - CSVPrinter 仅从标题中删除引号

转载 作者:行者123 更新时间:2023-12-05 01:59:21 24 4
gpt4 key购买 nike

当值被引用时,我需要在模式下使用 Apache 的 commons 中的 CSVPrinter,但标题不是。看起来只有报价模式选项,影响标题和值。这可以独立完成吗?

CSVFormat format = CSVFormat.DEFAULT.withHeader(new String(){"a", "b"})
.withQuoteMode(QuoteMode.ALL);
CSVPrinter printer = new CSVPrinter(new FileWriter(new File("out.csv")), format);
printer.printRecord("a val", "b val");
printer.printRecord("1", "2");
printer.flush();
printer.close();

给出:

"a", "b"
"a val", "b val"
"1", "2"

但是要求是这样的:

a,b
"a val", "b val"
"1", "2"

最佳答案

您可以对标题使用一种格式,对记录使用另一种格式:

FileWriter writer = new FileWriter(new File("out.csv"));
CSVFormat formatHeader = CSVFormat.DEFAULT
.withHeader(new String[]{"a", "b"})
.withEscape('"').withQuoteMode(QuoteMode.NONE);
CSVFormat formatRecord = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL);
CSVPrinter headerPrinter = new CSVPrinter(writer, formatHeader);
headerPrinter.flush();
CSVPrinter recordPrinter = new CSVPrinter(writer, formatRecord);
recordPrinter.printRecord("a val", "b val");
recordPrinter.printRecord("1", "2");
recordPrinter.flush();
recordPrinter.close();
headerPrinter.close();

关于java - CSVPrinter 仅从标题中删除引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67902352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com