gpt4 book ai didi

OPENCSV 将第一列填充为空

转载 作者:行者123 更新时间:2023-12-01 15:48:15 28 4
gpt4 key购买 nike

我正在尝试使用 HeaderColumnNameMappingStrategy 使用 opencsv 读取 csv 文件。 Opencsv 每次都将我的第一列 csv 填充为 null。
这是我的代码:

实体类:

@Entity
@Table(name = "MyEntity")
public class MyEntity {

@CsvBindByName(column = "status", required = true)
@Column(name = "status", length = 5, nullable = false)
private String status;

@CsvBindByName(column = "type", required = true)
@Column(name = "type", length = 5, nullable = false)
private String type;

@CsvBindByName(column = "percentage", required = true)
@Column(name = "percentage", nullable = false)
private Integer percentage;

@Column(name = "date", nullable = false)
@CsvBindByName(column = "date", required = true)
@CsvDate(value = "yyyy-MM-dd")
private Date date;
}

阅读逻辑:
 final HeaderColumnNameMappingStrategy<MyEntity> mpngInfo= new HeaderColumnNameMappingStrategy<>();
mappingInfo.setType(MyEntity.class);
final CsvToBean<MyEntity> csvToBean = new CsvToBean<>();
File file = new File(<path>);
CSVReader reader = new CSVReader(new FileReader(file),';');
final List<MyEntity> list = csvToBean.parse(mpngInfo, reader, true);
//here in list the first column is always populated as null.

我的CSV:

类型;状态;百分比;日期
活跃;美元;25;2014-01-01
活跃;英镑;25;2014-01-01

注意:即使我用“百分比”切换“类型”,它也会开始将百分比填充为空,因为“百分比”列成为第一列。

我想出了文件的第一个字符的问题。
就我而言,它是“”类型;状态;百分比;日期

问题列在这里:
First character of the reading from the text file : 

我该如何在 opencsv 中解决这个问题?有什么建议 ?

最佳答案

我遇到了同样的问题。

解决方案

我再次通过 utf-8 保存了 csv 文件 无 BOM .
然后将第一列设置为正确的值。
无需更改任何代码。
我用的是sakura的文本编辑器。选择编码为utf-8后,启用了BOM的复选框。取消选中它并保存csv文件。我认为其他编辑器具有相同的BOM选项。

原因

A BOM-ed UTF-8 string will start with the three following bytes. EF BB BF



添加到 csv 文件的三个字节可能会更改您的第一列名称。

关于OPENCSV 将第一列填充为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42328597/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com