我有以下数据
ID1,ID2,FIRST_NAME,LAST_NAME,BIRTH_DATE,HA1,HA2,HA3,STATUS,DT
99,13863926H,MAL"COLMHS,ABBOT,1997-04-09,AMKC,RR,DE,89,12973388H,"SAGAR,TARLE",AbdaT,1997-11-02,RNDC,71,88JunkTest,Howdy,Doody,1985-11-02,HA,HACLASSTYPE,2019-12-25
我正在尝试使用开放CSV解析csv,其中CSV中的名字可以包含双引号(MAL“ COLMHS)或带逗号的双引号(” SAGAR,TARLE“)或不带双引号的名字。 / p>
因此,使用.withIgnoreQuotations(true),我可以解析第一行(MAL“ COLMHS),但无法找到解析第二行的解决方案。
我尝试了具有多个StackOverflow链接的解决方案,但无法解决它们。
我知道我的CSV文件不一致,但是客户端的CSV文件中存在太多此类记录,很难手动使其一致,因此尝试搜索自动化解决方案。
List<Results> beans = new CsvToBeanBuilder<Results>(new FileReader(file.getabsolutePath()))
.withType(Results.class)
.withIgnoreQuotations(true)
.build().parse();
错误
java.lang.RuntimeException: Error parsing CSV line: 3. [3491903139,SAGAR,TARLE,]
at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:366)
at com.apds.partner.nycdoc.main.NycDocApplication.main(NycDocApplication.java:81)
at sun.reflect.NativeMethodaccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodaccessorImpl.invoke(NativeMethodaccessorImpl.java:62)
at sun.reflect.DelegatingMethodaccessorImpl.invoke(DelegatingMethodaccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: com.opencsv.exceptions.CsvRequiredFieldEmptyException: Number of data fields does not match number of headers.
at com.opencsv.bean.HeaderColumnNameMappingStrategy.verifyLineLength(HeaderColumnNameMappingStrategy.java:110)
at com.opencsv.bean.AbstractMappingStrategy.populateNewBean(AbstractMappingStrategy.java:313)
at com.opencsv.bean.concurrent.ProcessCsvLine.processLine(ProcessCsvLine.java:132)
at com.opencsv.bean.concurrent.ProcessCsvLine.run(ProcessCsvLine.java:85)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
*****
修改: 我也尝试了SupercsV,但是还是同样的问题