如何使用 CSVWriter 仅在 csv 中的嵌入式逗号字符串上使用双引号?
How to have double quotes only on embedded comma String in csv using CSVWriter?
这是我在输出 csv 中预期的结果格式:
header1,header2,header3,header4
monica,chandler,"rachel,ross",joey
我目前的成绩:
策略 1:
CSVWriter csvWriter = new CSVWriter(new FileWriter("D:/new.csv"));
String[] headers = new String[] { "header1", "header2", "header3", "header4"};
String[] arr = new String[] { "monica", "chandler", "rachel,ross", "joey"};
result:
"header1","header2","header2","header4"
"monica","chandler","rachel,ross","joey"
策略2:
CSVWriter csvWriter = new CSVWriter(new FileWriter("D:/new.csv"), CSVWriter.DEFAULT_SEPARATOR,CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);
String[] headers = new String[] { "header1", "header2", "header3", "header4"};
String[] arr = new String[] { "monica", "chandler", "rachel,ross", "joey"};
result:
header1,header2,header2,header4
monica,chandler,rachel",ross,joey
任何人都可以向我建议如何使用 OpenCSV 框架的 CSVWriter 实现此目的吗?
您可以使用 CsvWriter::writeNext
方法,该方法还需要一个 boolean
来指示是始终使用引号还是仅在需要时使用引号。
applyQuotesToAll
- True if all values are to be quoted. False applies quotes only to values which contain the separator, escape, quote, or new line characters.
一个例子:
try (CSVWriter csvWriter = new CSVWriter(new FileWriter("new.csv"))) {
String[] headers = new String[]{"header1", "header2", "header3", "header4"};
String[] arr = new String[]{"monica", "chandler", "rachel,ross", "joey"};
csvWriter.writeNext(headers, false);
csvWriter.writeNext(arr, false);
}
结果:
header1,header2,header3,header4
monica,chandler,"rachel,ross",joey
这是我在输出 csv 中预期的结果格式:
header1,header2,header3,header4
monica,chandler,"rachel,ross",joey
我目前的成绩:
策略 1:
CSVWriter csvWriter = new CSVWriter(new FileWriter("D:/new.csv"));
String[] headers = new String[] { "header1", "header2", "header3", "header4"};
String[] arr = new String[] { "monica", "chandler", "rachel,ross", "joey"};
result:
"header1","header2","header2","header4"
"monica","chandler","rachel,ross","joey"
策略2:
CSVWriter csvWriter = new CSVWriter(new FileWriter("D:/new.csv"), CSVWriter.DEFAULT_SEPARATOR,CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);
String[] headers = new String[] { "header1", "header2", "header3", "header4"};
String[] arr = new String[] { "monica", "chandler", "rachel,ross", "joey"};
result:
header1,header2,header2,header4
monica,chandler,rachel",ross,joey
任何人都可以向我建议如何使用 OpenCSV 框架的 CSVWriter 实现此目的吗?
您可以使用 CsvWriter::writeNext
方法,该方法还需要一个 boolean
来指示是始终使用引号还是仅在需要时使用引号。
applyQuotesToAll
- True if all values are to be quoted. False applies quotes only to values which contain the separator, escape, quote, or new line characters.
一个例子:
try (CSVWriter csvWriter = new CSVWriter(new FileWriter("new.csv"))) {
String[] headers = new String[]{"header1", "header2", "header3", "header4"};
String[] arr = new String[]{"monica", "chandler", "rachel,ross", "joey"};
csvWriter.writeNext(headers, false);
csvWriter.writeNext(arr, false);
}
结果:
header1,header2,header3,header4
monica,chandler,"rachel,ross",joey