我一直在嘗試解析groovy中的csv文件,目前使用庫org.apache.commons.csv 2.4。我的要求是,在csv單元格中有無效的數據值,比如無效字符,而不是在第一個無效行/單元格上拋出異常,我想收集這些異常並在csv文件中迭代直到結束,那麼我會得到這個csv文件有無效數據的完整列表。在異常容忍的Groovy中解析CSV
爲了達到這個目的,我嘗試了多種方法來使用這個apache庫,但不幸的是,只要它使用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"
第二個數據行有無效字符"
,使得解析器拋出異常
你能給格式和「無效字符」的例子嗎? – jalopaba