opencsv写入文件,其中某些引用的元素未引用 [英] opencsv writing file with some quoted elements and others unquoted
本文介绍了opencsv写入文件,其中某些引用的元素未引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人有使用Java中的opencsv编写csv文件的经验,其中仅某些元素需要用双引号引起来?我想要测试的理想输出是制作一个文件,内容为:
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,两个",三个
但是当我尝试以下代码时
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();
发生以下输出
1,"两个",三个
有想法吗?
推荐答案
这些额外的引号是 OpenCSV
使用的转义字符.您需要使用
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);
...
输出:
1,"two",three
这篇关于opencsv写入文件,其中某些引用的元素未引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文