gpt4 book ai didi

java - CSV阅读器空值java

转载 作者:行者123 更新时间:2023-12-01 12:47:44 24 4
gpt4 key购买 nike

我有这个 CSV:

0,102000082,,2,125,'Battery location','Left-hand drive',2,2
0,300000029,102000082,3,895,'Behind the cab','Left',2,-7
0,102000082,,4,127,'Battery location','Right-hand drive',4,4
^-----

我使用 csvReader 映射到 bean

    public static List<BatteryBean> loadAndRead{
File csvFilename = new File("C:\\my.csv");
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));

ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(BatteryBean.class);
String[] columns = new String[] { "ktypnr","sentenceId","parentId","sortOrder", "adjItemNoteId","sentText","itemNoteText","parentSortOrder1","parentSortOrder10" };
strat.setColumnMapping(columns);
CsvToBean csv = new CsvToBean();
List<BatteryBean> list = csv.parse(strat, csvReader);
return list;
}


public static void main(String[] args) {

try {
List<BatteryBean> list = loadAndRead("C:\\work\\battery_report_raw.csv");

for (Object object : list) {
BatteryBean bb = (BatteryBean) object;
System.out.println(bb.getKtypnr());
}
}

所以问题是文件在 ,, 之间包含空字符串,并且我在解析时遇到异常:

引起:java.lang.NumberFormatException:对于输入字符串:“”

我解决了。我还有一个问题

Csv file
ktypnr sentence_id parent_id sort_order adj_item_note_id sent_text iem_note_text
0 102000082 2 125 Battery location' Left-hand drive'
0 300000029 102000082 3 895 Behind the cab' Left'
0 102000082 4 127 Battery location' Right-hand drive'
0 300000029 102000082 5 898 Behind the cab' Right'

因此,如果一个entence_id =一个parent_id,我应该将这两个结合起来,这样看起来像这样(例如第一行和第二行),但我还应该考虑排序顺序:

0, Battery location, Left-hand drive, Behind the cab, Left

我不知道如何继续

最佳答案

将 BatteryBean 中的 parentId 更改为 String 数据类型。看来是整数。

关于java - CSV阅读器空值java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24525846/

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