2

我无法理解 apache commons csv 库的核心。当我想通过 apache-commons-csv 从 csv 文件中按标题名称获取列时,您能否建议我应该使用的方法?例如,我有这样的 csv 文件:

Delays, Computation time, Communication time
0., 500000, 10
20., 563210, 20
55., 546541., 23.
12., 465487., 23.
45., 456547., 28.
87., 985458., 56.
47., 456778., 45.

我想获取数组:double[] delays, double[] compTime, double[] commTime 由 csv 中的值组成。

4

2 回答 2

1
public void parseInputCSV() throws IOException {
final Reader reader = new InputStreamReader(inputFile.toURL().openStream(), "UTF-8");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(reader);
for (CSVRecord record : records) {
    String first = record.get("FIRST_COLUMN");
    String second = record.get("SECOND_COLUMN");
    // all other columns
}

}

于 2016-04-04T12:46:08.220 回答
0

在文档中:

Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record : records) {
  String lastName = record.get("Last Name");
  String firstName = record.get("First Name");
}

您将为每一行获得与列名对应的单元格。(这里 lastName 和 firstName 没有被使用,并且在每次迭代中被删除,当然)。

于 2016-04-04T12:45:09.573 回答