简体   繁体   English

如何使用Apache Commons CSV Java忽略CSV文件最后一行中的记录?

[英]How to ignore a record in the last line of the CSV file using Apache Commons CSV java?

I'm using Apache Commons CSV to read a CSV file. 我正在使用Apache Commons CSV读取CSV文件。 The file have an information about the file itself (date and time of generation) at the last line. 该文件在最后一行有关于文件本身(生成日期和时间)的信息。

|XXXX                                |XXXXX|XXXXX|XXXX|
|XXXX                                |XXXXX|XXXXX|XXXX|
|File generation: 21/01/2019 17.34.00|     |     |    |

So while parsing the file, I'm getting this as a record(obviously). 因此,在解析文件时,我将其作为记录(显然)。 I'm wondering is there any way to get rid of it from parsing and does Apache Commons CSV have any provision to handle it. 我想知道有没有办法摆脱解析,Apache Commons CSV有任何处理它的条款。

Apache Commons CSV provides a function to ignore the header ( https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#withSkipHeaderRecord-- ), but don't offer a solution to ignore the footer . Apache Commons CSV提供了忽略标题的功能( https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#withSkipHeaderRecord-- ),但不提供一个忽略footer的解决方案。 But you can simply get all records, except the last one by manually ignoring the last record. 但是您可以通过手动忽略最后一条记录来简单地获取除最后一条之外的所有记录。

It's a while loop and you wouldn't know when you get to the end until you get to the end. 这是一个循环,你不会知道什么时候到达结束直到你结束。 You have two options: 您有两种选择:

  • Bad option: Read it once and count the number of lines and then when you read it the second time you can break the loop when you reach (counter-1) line. 错误的选择:只读取一次并计算行数,然后第二次阅读时,到达(counter-1)行时可能会break循环。
  • Good option: It seems like your files are pipe delimited so when you're processing line by line simply make sure that line.trim().spit("|").length() > 1 or in your case do some work as long as the number of records per line is greater than 1. This will ensure you don't apply your logic on the lines with just one column which happens to be your last row aka footer. 好的选择:看起来你的文件是管道分隔的,所以当你逐行处理时,只需确保line.trim().spit("|").length() > 1或在你的情况下做一些工作只要每行的记录数大于1,这将确保您不会在只有一列(恰好是最后一行又称为页脚)的行上应用逻辑。

Example taken from Apache commons and modified a litte 取自Apache Commons并修改了样例的示例

Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);
for (CSVRecord record : records) {
    //all lines except the last will result greater than 1
    if (record.size() > 1){ 
        //do your work here 
        String columnOne = record.get(0);
        String columnTwo = record.get(1);
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM