opencsv writing file with some quoted elements and others unquoted

核能气质少年 提交于 2019-12-01 20:42:39

问题


Does anyone have experience using opencsv in Java to write a csv file where only some of the elements need to be double quoted? The desired output I am looking to test is to make a file that would read:

1,"two",three

but when I try the following code

writer = new CSVWriter(new FileWriter("yourfile.csv"), ',',CSVWriter.NO_QUOTE_CHARACTER);
String[] entries = {"1","\"two\"","three"};
writer.writeNext(entries);
writer.close();

the following output occurs

1,""two"",three

Thoughts?


回答1:


These extra quotes are the escape characters used by OpenCSV. You need to use the overloaded constructor that allows you to turn off these:

writer = new CSVWriter(new FileWriter("yourfile.csv"), ',', 
           CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER);
...

Output:

1,"two",three


来源:https://stackoverflow.com/questions/14531827/opencsv-writing-file-with-some-quoted-elements-and-others-unquoted

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!