在Java中使用Apache Commons写CSV时包括双引号

11

我正在使用apache commons CSV来写入csv文件。我想继续使用这个库。当我写一个csv文件时,在生成的文件的第一列中,它包含双引号作为引号字符,而其他列按预期生成。

我真的想在这里去掉双引号。请查看下面的代码。

CSVFormat format = CSVFormat.DEFAULT;
FileWriter fw = new FileWriter("Temp.csv");
CSVPrinter printer = new CSVPrinter(fw, format);

String[] temp = new String[4];

for(int i=0; i<4; i++) {
    if(i==1)
        temp[0] = "#";
    else
        temp[0] = "";
    temp[1] = "hello" + (i+1);
    temp[2] = "";
    temp[3] = "test";

    Object[] temp1 = temp[]
    printer.printRecord(temp1);
}

fw.close();
printer.close();

Temp.csv

"",hello1,,test
"#",hello2,,test
"",hello3,,test
"",hello4,,test

我不想每行开头都有引号,我只想要一个没有引号的空字符串,就像第三列一样。是否有人可以帮忙解决?


我知道这个问题已经有5年了,但是第一个字段输出为空字符串。这就是为什么有两个引号的原因。当我开始使用CSV文件时,所有文本字段都被引号包围,这很有趣。也许是因为当时我主要在Microsoft领域工作,所以我变得麻木了。现在我回来了,似乎事情变得更加松散了。 - David Bradley
2个回答

13

在lars问题跟踪中提到,尝试将CSVFormat设置为以下内容:

final CSVFormat csvFileFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE);


尝试在1.5版本上运行,你知道这个程序可以在哪个版本上运行吗? - lorraine batol

8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接