現在ライブラリorg.apache.commons.csv 2.4を使用して、groovyでcsvファイルを解析しようとしています。私が持っている要件は、csv セルに無効な文字などの無効なデータ値があり、最初の無効な行/セルで例外をスローする代わりに、これらの例外を収集し、最後まで csv ファイルで反復し続けたいということです。次に、この csv ファイルに含まれる無効なデータの完全なリストが表示されます。
その目的で、この apache lib を使用する複数の方法を試しましたが、残念ながら反復に CSVParser.getNextRecord() を使用している限り、反復子は中止されます。
次のようなコードを入力します。
def records = new CSVParser(reader, CSVFormat.EXCEL.withHeader().withIgnoreSurroundingSpaces())
// at this line, the iterator() inside CSVParser is always using getNextRecord() for its next() implementation, and it may throw exception on invalid char
records.each {record->
// if the exception is thrown from .each, that makes below try/catch in vain
try{
}catch(e){ //want collect Errors here }
}
では、この図書館で掘り下げるべきものは他にありますか? または、誰かが私に別のより実行可能な解決策を教えてもらえますか? みんなに感謝します!
更新: サンプル CSV
"Company code for WBS element","WBS Element","PS: Short description (1st text line)","Responsible Cost Center for WBS Element","OBJNR","WBS Status"
"1001","RE-01768-011","Opex - To present a paper on Career con","0000016400","PR00031497","X"
"1001","RE-01768-011","Opex - To present a paper on "Career con","0000016400","PR00031497","X"
2 番目のデータ行に無効な文字"
が含まれているため、パーサーは例外をスローします