我有两个csv源可以读取数据。两个csv文件提供相同的数据,但名称或列位置不同。是否有方法将@CsvBindByByName添加为"OR“。例如,两个文件的头如下所示
CSV 1- RollNo,StudentName,等级,年龄,PrimaryLanguage,ProjectName CSV 2-名称,类别,PrimLang,EnrollmentNumber,年龄,项目,AttendancePercentage
我必须在同一个POJO中读取两个csv文件信息。
public class StudentInfo{
@CsvBindByName(column = "RollNo")
private String rollNo;
@CsvBindByName(column = "StudentName")
private String studentName;
@CsvBindByName(column = "PrimaryLanguage")
private String primaryLanguage;
@CsvBindByName(column = "Class")
private String class;
@CsvBindByName(column = "ProjectName")
private String projectName;
@CsvBindByName(column = "Age")
private String age;
//getters and setters
}
或者有其他的方法来实现这一点。如果我只采用一种csv头格式,我的代码就能正常工作。谢谢
发布于 2021-07-26 14:56:27
opencsv中的
Profiles
解决了这个问题,但它只能从5.4中获得。
见
文档
on
Profiles
下面是官方文档中的代码片段。
public class Person {
@CsvBindByNames({
@CsvBindByName(column = "last name"),
@CsvBindByName(profiles = {"customer 2", "customer 5"})
private String surname;
@CsvBindByNames({
@CsvBindByName,
@CsvBindByName(column = "first name", profiles = "customer 1"),
@CsvBindByName(column = "given name", profiles = "customer 2")
private String name;
@CsvIgnore(profiles = "customer 2")
@CsvBindByName(column = "middle initial")
private char initial;
@CsvBindByName(column = "salary", profiles = "customer 1")
@CsvBindByName(column = "annual salary", profiles = "customer 2")
@CsvNumber(value = "#0.00", profiles = "customer 1")
@CsvNumber(value = "0.0#E0", profiles = "customer 2")
private float salaryInUSD;
@CsvBindByName(column = "height")
@CsvNumbers({
@CsvNumber("000"),
@CsvNumber(value = "000cm", profiles = "customer 2")
private int heightInCentimeters
// Accessor methods go here.
// Now, the profile can be used in the builder parse, see below.