我想解析一份 Google 电子书交易报告。我在 Notepad++ 中打开它以准确查看归档和记录分隔符。它是一个制表符分隔的文件,每个标题字段和数据字段都用引号引起来。CSV 文件的前两行是:
“交易日期”“ID”“产品”“类型”“预购”“数量”“主要 ISBN”“版本说明名称”“标题”“作者”“原始标价货币”“原始标价”“标价货币”“标价 [含税]" "标价 [不含税]" "销售国家/地区" "出版商收入 %" "出版商收入" "付款货币" "付款金额" "货币兑换率" “2016. 09. 01.” "ID:1166315449551685" "单次购买" "销售" "无" "1" "9789633780664" "Book and Walk Kft" "Bánk bán" "József Katona" "HUF" "0,00" "HUF" "0,00 " "0,00" "HU" "52,0%" "0,00" "" "" ""
我使用以下代码解析 CSV 文件:
private List<Sales> parseCsv(File csv) {
Calendar max = Calendar.getInstance();
Calendar current = Calendar.getInstance();
boolean firstRound = true;
List<Sales> sales = new ArrayList<>();
Sales currentRecord;
Reader in;
try {
in = new FileReader(csv);
Iterable<CSVRecord> records;
try {
records = CSVFormat.TDF.withQuote('\"').withFirstRecordAsHeader().parse(in);
for (CSVRecord record : records) {
currentRecord = new Sales();
currentRecord.setAuthor(record.get("Author"));
currentRecord.setTitle(record.get("Title"));
currentRecord.setPublisher(record.get("Imprint Name"));
currentRecord.setIsbn(record.get("Primary ISBN"));
currentRecord.setChannel("Google");
currentRecord.setBookId(record.get("Id"));
currentRecord.setCountry(record.get("Country of Sale"));
currentRecord.setUnits(Integer.parseInt(record.get("Qty")));
currentRecord.setUnitPrice(Float.parseFloat(record.get("List Price [tax exclusive]")));
Date transDate;
try {
transDate = sourceDateFormat.parse(record.get("Transaction Date"));
if (firstRound) {
max.setTime(transDate);
};
current.setTime(transDate);
if (current.after(max)) {
max.setTime(current.getTime());
}
currentRecord.setDatum(transDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
LOG.log(Level.SEVERE,"Nem megfeelő formátumú a dátum a {0} file-ban",csv.getAbsolutePath());
}
currentRecord.setCurrencyCustomer(record.get("List Price Currency"));
currentRecord.setCurrencyProceeds(record.get("Payment Amount"));
currentRecord.setCurrencyProceeds(record.get("Payment Currency"));
sales.add(currentRecord);
}
LOG.log(Level.INFO, "Daily sales transactions of {0} were successfully parsed from ",
csv.getAbsolutePath());
return sales;
} catch (IOException e1) {
// TODO Auto-generated catch block
LOG.log(Level.SEVERE, "Valami nem stimmel a {0} file szerkezetével",csv.getAbsolutePath());
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
LOG.log(Level.SEVERE,"A {0} file-t nem találom.",csv.getAbsolutePath());
}
return null;
};
当我调试解析过程时,我可以看到 record.get("Author") 引发了运行时异常:
java.lang.IllegalArgumentException: Mapping for Author not found, expected one of [��"
显然我有名为作者的列。知道出了什么问题吗?