我正在使用org.apache.commons-csv 1.4,本周我在一个junit测试中发现了这个奇怪的行为:
CSVReader reader = null;
List<String[]> linesCsv = new ArrayList<>();
FileInputStream fileStream = null;
InputStreamReader inputStreamReader = null;
try {
fileStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(fileStream, "ISO-8859-1");
reader = new CSVReader(inputStreamReader, ',', '"', 0);
String[] record = null;
while ((record = reader.readNext()) != null) {
linesCsv.add(record);
}
} catch (Exception e) {
logger.error("Error in ", e);
} finally {
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (fileStream != null) {
fileStream.close();
}
if (reader != null) {
reader.close();
}
}
*错误案例
输入.csv
DAR_123451 ,"XXXXX Hello World "Hello World XXX "
DAR_123452 ,"XXXXX Hello World "Hello World XXX "
Java KO:
[0.0] DAR_123451
[0.1] XXXXX Hello World“Hello World XXX \ nDAR_123456,XXXXX Hello World”Hello World XXX
*正确的案例
输入.csv
DAR_123451 ,"XXXXX Hello World "Hello World" XXX "
DAR_123452 ,"XXXXX Hello World "Hello World" XXX "
Java OK:
[0.0] DAR_123451 [0.1] XXXXX Hello World“Hello World”XXX
[1.0] DAR_123452 [1.1] XXXXX Hello World“Hello World”XXX
我无法设置公共csv库才能正常工作,看起来它是一个Bug,我们如何正确读取字符串中的单引号字符串?
答案 0 :(得分:1)
如果值被引号包围,则CSV格式通常使用2个连续的双引号在文本中包含双引号,例如以下作品。
当我使用最新版本的commons-csv时,我的输入会出现异常(IOException: (line 1) invalid char between encapsulated token and delimiter
)
因此,要正确包含使用以下
所需的双引号DAR_123451 ,"XXXXX Hello World ""Hello World"" XXX "
DAR_123452 ,"XXXXX Hello World ""Hello World"" XXX "
然后测试用例按预期工作:
Reader in = new StringReader(
"DAR_123451 ,\"XXXXX Hello World \"\"Hello World XXX\"\" \"\n" +
"DAR_123452 ,\"XXXXX Hello World \"\"Hello World XXX\"\" \"");
Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
for (CSVRecord record : records) {
for (int i = 0; i < record.size(); i++) {
System.out.println("At " + i + ": " + record.get(i));
}
}
输出:
At 0: DAR_123451
At 1: XXXXX Hello World "Hello World XXX"
At 0: DAR_123452
At 1: XXXXX Hello World "Hello World XXX"
有关详细信息,请参阅https://en.wikipedia.org/wiki/Comma-separated_values#General_functionality。