Escape special meaning of # (double quote) in Apache commons-csv

346 Views Asked by At

HELLO printing in a double quote in .tsv file because of '#' like "#HELLO". I want to remove the double quotes when it writes to .tsv

current output -> "#HELLO"

expected output -> #HELLO

try ( CSVPrinter printer = new CSVPrinter(new FileWriter("data.tsv"), CSVFormat.TDF))
{
   printer.printRecord("#HELLO");

}catch (IOException ex) {
    ex.printStackTrace();
}
        
2

There are 2 best solutions below

0
shreeshailaya vitkar On BEST ANSWER

I changed the version of CSVCommons to 1.8 and below code is working for me...

CSVPrinter printer = new CSVPrinter(new FileWriter("data.tsv"), CSVFormat.TDF.withHeader("#default1", "default2").withEscape('"').withQuoteMode(QuoteMode.NONE)
2
talex On

You need to adjust format

CSVFormat.Builder.create(CSVFormat.TDF)
    .withQuoteMode(QuoteMode.MINIMAL)
    .build();