使用opencsv (java)读取.csv文件时跳过空行

时间:2022-03-15 23:16:05

Good day everyone! My target is to make csv reader to skip the blank lines while parsing a file, do nothing basically, only get me the rows with at least one value. At the moment I have two methods -> 1st is just reading all rows as List of Strings array and returns it, 2nd converts the result into List of Lists of Strings, both are bellow:

美好的一天!我的目标是让csv reader在解析文件时跳过空行,基本上什么都不做,只获取至少一个值的行。目前我有两种方法——>第一种方法是将所有的行作为字符串数组的列表读取并返回,第二种方法将结果转换为字符串列表,它们都是下面的:

private List<String[]> readCSVFile(File filename) throws IOException {

    CSVReader reader = new CSVReader(new FileReader(filename));
    List<String[]> allRows = reader.readAll();

    return allRows;

}

public List<List<String>> readFile(File filename) throws IOException {

        List<String[]> allRows = readCSVFile(filename);     
        List<List<String>> allRowsAsLists = new ArrayList<List<String>>();      
        for (String[] rowItemsArray :  allRows) {
            List<String> rowItems = new ArrayList<String>();
            rowItems.addAll(Arrays.asList(rowItemsArray));
            allRowsAsLists.add(rowItems);

        }
    return allRowsAsLists;

}

My first thought was to check (in the 2'nd method) the length of an array if its 0 just to ignore it - which would be something like this:

我的第一个想法是检查(在2'nd方法中)一个数组的长度,如果它的0只是为了忽略它——应该是这样的:

for (String[] rowItemsArray :  allRows) {
            **if(rowItemArray.length == 0) continue;**
            List<String> rowItems = new ArrayList<String>();
            rowItems.addAll(Arrays.asList(rowItemsArray));
            allRowsAsLists.add(rowItems);

}  

Unfortunately that didn't work for the reason that even if the row is blank it still returns an array of elements - empty Strings in fact. Checking an individual String is not an option as there are 100+ columns and this is variable. Please suggest what’s the best way to achieve this. Thanks.

不幸的是,这并没有起作用,因为即使行是空的,它仍然返回一个元素数组——实际上是空字符串。检查单个字符串不是一个选项,因为有100+列,这是变量。请提出实现这一目标的最佳方式。谢谢。

Sorted it out this way:

以这种方式进行分类:

    public List<List<String>> readFile(File filename) throws IOException {

            List<String[]> allRows = readCSVFile(filename, includeHeaders, trimWhitespacesInFieldValues);       
            List<List<String>> allRowsAsLists = new ArrayList<List<String>>();      
            for (String[] rowItemsArray :  allRows) {
                **if(allValuesInRowAreEmpty(rowItemsArray)) continue;**
                List<String> rowItems = new ArrayList<String>();
                rowItems.addAll(Arrays.asList(rowItemsArray));
                allRowsAsLists.add(rowItems);

            }
            return allRowsAsLists;

        }

    private boolean allValuesInRowAreEmpty(String[] row) {
        boolean returnValue = true;
        for (String s : row) {
            if (s.length() != 0) {
                returnValue = false;
            }
        }
        return returnValue;
    }

2 个解决方案

#1


5  

You could check the length and the first element. If the line contains only a field separator then the lenght > 1. If the line contains a single space character then the first element is not empty.

你可以检查长度和第一个元素。如果该行仅包含字段分隔符,则lenght > 1。如果行包含一个空格字符,那么第一个元素不是空的。

if (rowItemsArray.length == 1 && rowItemsArray[0].isEmpty()) {
    continue;
}

#2


0  

You could summarize all string values per row after trimming them. If the resulting string is empty, there are no values in any cell. In that case ignore the line.
Something like this:

您可以对每一行进行修剪后的所有字符串值进行汇总。如果结果字符串为空,则任何单元格中都没有值。在这种情况下,忽略这条线。是这样的:

private boolean onlyEmptyCells(ArrayList<String> check) {
    StringBuilder sb = new StringBuilder();
    for (String s : check) {
        sb.append(s.trim());
    }
    return sb.toString().isEmpty(); //<- ignore 'check' if this returns true
}

#1


5  

You could check the length and the first element. If the line contains only a field separator then the lenght > 1. If the line contains a single space character then the first element is not empty.

你可以检查长度和第一个元素。如果该行仅包含字段分隔符,则lenght > 1。如果行包含一个空格字符,那么第一个元素不是空的。

if (rowItemsArray.length == 1 && rowItemsArray[0].isEmpty()) {
    continue;
}

#2


0  

You could summarize all string values per row after trimming them. If the resulting string is empty, there are no values in any cell. In that case ignore the line.
Something like this:

您可以对每一行进行修剪后的所有字符串值进行汇总。如果结果字符串为空,则任何单元格中都没有值。在这种情况下,忽略这条线。是这样的:

private boolean onlyEmptyCells(ArrayList<String> check) {
    StringBuilder sb = new StringBuilder();
    for (String s : check) {
        sb.append(s.trim());
    }
    return sb.toString().isEmpty(); //<- ignore 'check' if this returns true
}