2017-01-02 25 views
3

嗨我有一个CSV文件,其中的封装器字符未正确转义。解析CSV文件,其中内容中的封装器没有正确转义

[email protected],"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, 

分隔符 - >,

封装器 - >“

它打破了,当我尝试使用公地CSV读卡器, 抛出一个 'invalid char between encapsulated token and delimiter' 异常阅读

但是,Microsoft Excel似乎完美地打开文件。 关于如何procc的任何想法编辑? 。

如何解析封装程序未正确转义的CSV文件?.Excel似乎打开这样的文件很好。

+1

我发现这个有用http://stackoverflow.com/questions/15210568/java-csv-parser-with-unescaped-quotes自由职业办公室和Excel似乎是采取有教养的猜测。我怎样才能做到这一点? –

回答

1

如果你不能修复这个源头(即产生一个结构良好的CSV),并要在此分析自己,你可以去简单的方法:

扫描FIELD1高达," - 场2最多", - 其余是field3(尾随逗号?)。

当然,如果在html字段中出现",,则会出现问题。你可以通过首先扫描到,",然后倒退(从行的末尾开始)到",来解决这个问题。

如果有更多的领域比你在这里展示,你可以找一个,"组合(包括组合,也可能是","),并希望那些没有出现在现场数据。

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("" + 
      "[email protected],\"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); 
    } 

此打印:

[email protected] 
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许可证)

相关问题