使用Apache Commons CSV跳过CSV文件中的空记录 [英] To skip empty records from a CSV file using Apache Commons CSV

查看:293
本文介绍了使用Apache Commons CSV跳过CSV文件中的空记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果CSV文件包含三列,并且值如下所示

if a CSV file contains three columns and if the values are as given below

a,b,c
     //empty line
,,,
a,b,c

有是两个有效记录。使用Apache Commons CSV解析器,我可以轻松跳过具有空行的记录。但是,当记录仅包含空值时,如何跳过它呢?

There are two valid records. Using Apache commons CSV parser, i could easily skip the record which has empty lines. But when the records contain only null values, how to skip it then?

为了克服这个问题,我使用了 String equals()具有已构造的空记录。这是一个示例实现。

To overcome this, I'm using String equals() with already constructed empty record. Here is a sample implementation.

List<String[]> csvContentsList = new ArrayList<String[]>();
CSVFormat csvFormat = CSVFormat.DEFAULT.withNullString("");
CSVParser csvParser = new CSVParser(fileReader, csvFormat);

String[] nullRecordArray = { null, null, null};
String nullRecordString = Arrays.toString(nullRecordArray);
for (CSVRecord csvRecord : csvParser) {
    try {
        String values[] = { csvRecord.get(0),csvRecord.get(1),csvRecord.get(2) };
        if (!nullRecordString.equals(Arrays.toString(values))) //lineA
            csvContentsList.add(values);
    } catch (Exception e) {
        // exception handling
    }
}

当我不使用标记为'lineA'的行时,此实现在 csvContentsList 中给出以下三个记录

When i don't use the line marked as 'lineA', this implementation gives three records in the csvContentsList as below

[a,b,c]
[null,null,null]
[a,b,c]

是否存在任何内置方法?还是其他更好的方法?

Is there any inbuilt way to do this? or any other better way?

推荐答案

在此处找到另一种可能的解决方案。

Find here another possible solution.

CSVFormat csvFormat = CSVFormat.DEFAULT.withNullString("");
CSVParser csvParser = new CSVParser(fileReader, csvFormat);
for (CSVRecord csvRecord : csvParser.getRecords()) {
    String values[] = {csvRecord.get(0), csvRecord.get(1), csvRecord.get(2)};
    for (String value : values) {
        if (value != null) {
            // as soon a value is not-null we add the array
            // and exit the for-loop
            csvContentsList.add(values);
            break;
        }
    }
}

假定输入

a,b,c

,,,
d,e,f

输出

a,b,c
d,e,f

编辑如果可以使用Java 8,则可能是一种解决方案。

edit If you can use Java 8 a solution might be.

List<String[]> csvContentsList = csvParser.getRecords()
        .stream() 
        .sequential() // 1.
        .map((csvRecord) -> new String[]{
            csvRecord.get(0), 
            csvRecord.get(1), 
            csvRecord.get(2)
        }) // 2.
        .filter(v -> Arrays.stream(v)
                .filter(t -> t != null)
                .findFirst()
                .isPresent()
        ) // 3.
        .collect(Collectors.toList()); // 4.




  1. 如果行的顺序很重要

  2. 将csvRecord映射到String []

  3. 对具有至少一个非空值的String数组进行过滤

  4. 收集所有值并返回列表

  1. if the order of lines is important
  2. map a csvRecord to a String[]
  3. filter on String arrays with at least one non-null value
  4. collect all values and return a List

可能需要根据您的要求进行修改。

Might need to be amended, depending on your requirements.

这篇关于使用Apache Commons CSV跳过CSV文件中的空记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆