我有一个CSV,我通过构建StringBuilder和使用printwwriter写入生成。然后我再次读取CSV并追加一些内容,但它弄乱了单元格,单元格中有双引号,用于表示英寸。它打印两次双引号作为15"
添加到StringBuilder中的一个值是:
1.1代码String title = "Poly Nuclear 15" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory"
Text t1 = new Text();
t1.setContent(title);
if (title.contains(""")) {
t1.setContent("Poly Nuclear 15\" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory");
}
我使用printwwriter的第一个输出(在使用StringBuilder创建逗号分隔的字符串后)是这样的:
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filename, true), StandardCharsets.UTF_8);
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.println(stringBuilder.toString());
key,date,ms_id,title,alertId
190-2,2022-02-20 12:35:09,107193,Poly Nuclear 15" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory,
1.2代码现在我在每行的末尾添加最后一列的值alertId
。我读取并追加每行,然后写回CSV,如下所示:
// Here below method is called as writeBack("1222") with fixed value.
public void writeBack(String value) {
String filePath = "/dir1/dir2/test.csv";
String key = "alertId"; // column name for which value needs to be added.
InputStreamReader inputStreamReader = new InputStreamReader(new
FileInputStream(filePath), StandardCharsets.UTF_8);
CSVReader reader = new CSVReader(inputStreamReader);
String[] header = reader.readNext();
int columnNum = Arrays.asList(header).indexOf(key);
List<String[]> feedData = reader.readAll();
try {
for (String[] row : feedData) {
row[columnNum] = value;
}
reader.close();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8);
CSVWriter writer = new CSVWriter(outputStreamWriter);
writer.writeNext(header);
writer.writeAll(feedData);
writer.flush();
writer.close();
} catch (Exception e) {
writeLog("ERROR", e);
}
}
我的最终输出是这一切都是正确的,除了字符串值有双引号作为15""
"key","date","ms_id","title","alertId"
"190-2","2022-02-20 12:35:09","107193","Poly Nuclear 15"" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory","1222"
如何避免在表示英寸的单元格的最终输出中使用双引号?
预期输出
"key","date","ms_id","title","alertId"
"190-2","2022-02-20 12:35:09","107193","Poly Nuclear 15" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory","1222"
感谢@Mark Rotteveel的指针提示。这帮助我寻找一个不同的分隔符,也转义更多的字符。
意识到CSVReader和CSVWriter也有不同的转义字符。
我最后用下面的方法解决了:
import org.apache.commons.lang.StringEscapeUtils;
...
StringEscapeUtils.escapeCsv("Poly Nuclear 15" Laptop Series, Notebook \ Intel Windows10+ 7.6V Battery 8GB Memory")
在书写
时使用import com.opencsv.ICSVWriter;
...
char escapeChar = '\';
CSVWriter writer = new CSVWriter(outputStreamWriter, ICSVWriter.DEFAULT_SEPARATOR, ICSVWriter.DEFAULT_QUOTE_CHARACTER, escapeChar, ICSVWriter.DEFAULT_LINE_END);