您好我有一个CSV文件,其中封装器字符未正确转义。
实施例
uerf@einf.erf,"uhrege gerjhhg er<span style="background-color: rgb(0,153,0);">eriueiru kernger</span><font color="#009900"><span style="background-color: rgb(255,255,255);"> weiufhuweifbw fhew fibwefbw</span></font><div><font color="#009900"><span style="background-color: rgb(255,255,255);">wekifbwe fewf</span></font></div><div><font color="#009900"><span style="background-color: rgb(255,255,255);">weiuifgewbfjew f</span></font></div>",18-Oct-2016,
分隔符 - &gt; ,
封装器 - &gt; “
当我尝试使用commons-csv阅读器阅读时,它会中断
抛出'invalid char between encapsulated token and delimiter
'例外。
然而,Microsoft excel似乎完全打开了该文件。 关于如何进行的任何想法?
如何解析封装器未正确转义的CSV文件?.Excel似乎打开这样的文件。
答案 0 :(得分:1)
如果您无法在源头修复此问题(即生成格式正确的csv),并且您想自己解析它,那么您可以采用简单的方法:
将字段1扫描到,"
- 字段2到",
- 其余是字段3(尾随逗号?)。
当然如果html字段中出现",
,则会出现问题。您可以通过先扫描,"
,然后向后扫描(从行尾开始)到",
来解决这个问题。
如果您显示的字段多于此处显示的字段,则可以查找,
与"
相结合(两种组合,也可以是","
)并希望这些字段不会显示在现场数据。
答案 1 :(得分:0)
univocity-parsers有一个CSV解析器,可以正确处理这种输入。
//first configure the parser
CsvParserSettings settings = new CsvParserSettings();
settings.setUnescapedQuoteHandling(UnescapedQuoteHandling.STOP_AT_CLOSING_QUOTE);
//then create a parser and parse your input line:
CsvParser parser = new CsvParser(settings);
String[] result = parser.parseLine("" +
"uerf@einf.erf,\"uhrege gerjhhg er<span style=\"background-color: rgb(0,153,0);\">eriueiru kernger</span><font color=\"#009900\"><span style=\"background-color: rgb(255,255,255);\"> weiufhuweifbw fhew fibwefbw</span></font><div><font color=\"#009900\"><span style=\"background-color: rgb(255,255,255);\">wekifbwe fewf</span></font></div><div><font color=\"#009900\"><span style=\"background-color: rgb(255,255,255);\">weiuifgewbfjew f</span></font></div>\",18-Oct-2016,");
//here's the result (one value per line)
for (String v : result) {
System.out.println(v);
}
打印:
uerf@einf.erf
uhrege gerjhhg er<span style="background-color: rgb(0,153,0);">eriueiru kernger</span><font color="#009900"><span style="background-color: rgb(255,255,255);"> weiufhuweifbw fhew fibwefbw</span></font><div><font color="#009900"><span style="background-color: rgb(255,255,255);">wekifbwe fewf</span></font></div><div><font color="#009900"><span style="background-color: rgb(255,255,255);">weiuifgewbfjew f</span></font></div>
18-Oct-2016
null
希望它有所帮助。
免责声明:我是这个图书馆的作者。它是开源和免费的(Apache v2.0许可证)