2017-01-02 4 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, 

구분 ->

캡슐은 - 내가 는 'invalid char between encapsulated token and delimiter'예외를 throw 평민-CSV 리더를 사용하여 읽을 때> "

이 중단

.

그러나 Microsoft Excel에서 파일을 완벽하게 여는 것 같습니다. procc 방법에 대한 아이디어 에드? .

캡슐화자가 제대로 이스케이프 처리되지 않은 CSV 파일을 어떻게 구문 분석하나요? .Excel은 이러한 파일을 잘 여는 것 같습니다.

+1

나는이 유용한 것을 발견했다. http://stackoverflow.com/questions/15210568/java-csv-parser-with-unescaped-quotes 리브레 사무실과 엑셀이 될 것 같다. 교양있는 추측을한다. 어떻게 만들 수 있습니까? –

답변

1

당신이 (즉, 잘 구성된 CSV를 생성하는) 소스에서이 문제를 해결할 수 없으며, 당신이 직접 구문 분석하려면 쉬운 방법 갈 수 :

스캔 ,"까지 필드 1 - FIELD2을 최대 ", - 나머지는 필드 3 (후행 쉼표?)입니다.

물론 ",이 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 

는 도움이되기를 바랍니다.

면책 조항 : 본인은이 라이브러리의 저자입니다. 그것은 오픈 소스이고 자유 (아파치 v2.0 라이센스)