【问题标题】:CSV column value calculation in javajava中的CSV列值计算
【发布时间】:2016-09-03 04:17:09
【问题描述】:

我正在寻找可以简化 CSV 读取/写入及其值计算的 Java 开源 API。我有 3 个 CSV 文件,我需要原始读取并进行计算并写入最终的 CSV 文件。

【问题讨论】:

标签: java string file csv


【解决方案1】:

uniVocity-parsers 有一个很棒的 AP​​I,可以帮助您处理各种 CSV 输入。

这是一个读取文件中给定列集的示例:

CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial
parserSettings.selectFields("Foo", "Price", "Blah"); //get only those fields you are interested in
CsvParser parser = new CsvParser(settings);
List<Record> allRecords = parser.parseAllRecords(new File("/path/to/file.csv"));

double sumOfPrices = 0.0;
for (Record record : allRecords) {
    //Here we read the "price" column, using the "0,00" format mask, and decimal separator set to comma.
    Double price = record.getDouble("price", "0,00", "decimalSeparator=,");

    if (price != null) {
        sumOfPrices += price;
    }
}    

披露:我是这个库的作者。它是开源免费的(Apache V2.0 许可)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    相关资源
    最近更新 更多