opencsv:如何在单元格内用双引号解析数据?

时间:2017-09-05 17:13:20

标签: java parsing opencsv

我尝试使用opencsv(版本3.10)解析一些公共数据。这是一段抓取CSV并将记录映射到POJO列表的代码片段:

d3.selectAll('.myseries').selectAll('.seriesChild')

解析列表中的记录少于CSV中的记录。查看数据,我注意到单元格值中有时会出现双引号。这是一个例子:

URL permitsURL = new URL("http://assessor.boco.solutions/ASR_PublicDataFiles/Permits.csv");
InputStream permitInputStream = permitsURL.openStream();
Reader permitStreamReader = new InputStreamReader(permitInputStream);

CsvToBean<PermitRecord> csvToBean = new CsvToBean<PermitRecord>();

Map<String, String> columnMapping = new HashMap<String, String>();
columnMapping.put("strap", "strap");
columnMapping.put("issued_by", "issuedBy");
columnMapping.put("permit_num", "permitNum");
columnMapping.put("permit_category", "permitCategory");
columnMapping.put("issue_dt", "issueDt");
columnMapping.put("estimated_value", "estimatedValue");
columnMapping.put("description", "description");

HeaderColumnNameTranslateMappingStrategy<PermitRecord> strategy = new HeaderColumnNameTranslateMappingStrategy<PermitRecord>();
strategy.setType(PermitRecord.class);
strategy.setColumnMapping(columnMapping);

List<PermitRecord> permitRecordList = null;

CSVReader csvReader = new CSVReader(permitStreamReader);
permitRecordList = csvToBean.parse(strategy, csvReader);

36&#34; 下的套牌会导致后续记录进入描述。通过IDE查看时更为明显:

permit record IDE screenshot

你能看出我做错了什么吗?我怀疑它是一个简单的解决方案,因为它可以被Excel正确解析,而opencsv似乎是Java CSV解析的事实标准。

1 个答案:

答案 0 :(得分:1)

Univocity CSV parsers非常易于使用。将CSV列映射到POJO属性非常简单。

我将以下依赖项添加到pom.xml

<dependency>
    <groupId>com.univocity</groupId>
    <artifactId>univocity-parsers</artifactId>
    <version>2.5.4</version>
</dependency>

使用注释将CSV列映射到属性。请注意方便的注释:

  • Parsed(field = "abc"):将CSV列映射到变量
  • @Trim:删除前导/尾随空格
  • @Format(formats = {"MM/dd/yyyy"}):允许我们指定日期格式

这是POJO:

package io.woolford.entity;

import com.univocity.parsers.annotations.Format;
import com.univocity.parsers.annotations.Parsed;
import com.univocity.parsers.annotations.Trim;
import java.util.Date;

public class PermitRecord {

    @Trim
    @Parsed(field = "strap")
    private String strap;

    @Parsed(field = "issued_by")
    private String issuedBy;

    @Parsed(field = "permit_num")
    private String permitNum;

    @Parsed(field = "permit_category")
    private String permitCategory;

    @Format(formats = {"MM/dd/yyyy"})
    @Parsed(field = "issue_dt")
    private Date issueDt;

    @Parsed(field = "estimated_value")
    private Integer estimatedValue;

    @Parsed(field = "description")
    private String description;

    // getters & setters removed for brevity
}

然后,从CSV文件中的记录创建POJO列表:

URL permitsURL = new URL("http://assessor.boco.solutions/ASR_PublicDataFiles/Permits.csv");
InputStream permitInputStream = permitsURL.openStream();
List<PermitRecord> permitRecordList = new CsvRoutines().parseAll(PermitRecord.class, permitInputStream);

感谢@JeronimoBackes这个优雅的解决方案。感谢Univocity出色的CSV解析器。