1

How to Ignore empty lines? I am using the below snippet of code and it doesnt ignore the empty lines. Any pointer configuration available in CSV parser to fix this issue?

 public CSVParser parseCSV(InputStream inputStream) {
        try {
            return new CSVParser(new InputStreamReader(inputStream, StandardCharsets.UTF_8), CSVFormat.DEFAULT
                    .withFirstRecordAsHeader()
                    .withIgnoreHeaderCase()
                    .withSkipHeaderRecord()
                    .withIgnoreEmptyLines()
                    .withTrim());
        } catch (IOException e) {
            throw new IPRSException(e);
        }
    }

Sample file

h1,h2,h3
d1,d2,d3
,,,

Expected output

d1,d2,d3
7
  • 1
    Is ,,, an empty line or a non-empty line with no values? Commented Oct 11, 2018 at 6:34
  • its a empty line. I convert xlsx file to csv and when there is an empty line in xlsx, in CSV its ended up as ,,, Commented Oct 11, 2018 at 6:47
  • .withIgnoreEmptyLines doesnt seems to be working Commented Oct 11, 2018 at 6:47
  • So what's your actual output (as opposed to your expected output)? Commented Oct 11, 2018 at 7:00
  • actual output is : d1,d2,d3 Commented Oct 11, 2018 at 7:21

1 Answer 1

2

Apache CSV parser doesnt support empty lines out of the box, so ended up writing custom code.

private boolean isEmpty(CSVRecord csvRecord){
        if (null == csvRecord) return true;
        for (int i = 0; i < csvRecord.size(); i++) {
            if (StringUtils.isNotBlank(csvRecord.get(i))) {
                return false;
            }
        }
        return true;
    }


    public List<Map<String, Object>> getMapFromCSV(InputStream inputStream) {
        try {
            CSVParser parser = parseCSV(inputStream);
            return getMap(parser.getRecords().stream()
                    .sequential().filter(v -> !isEmpty(v))
                    .collect(Collectors.toList()), parser.getHeaderMap());
        } catch (IOException e) {
            throw new Exception(e);
        }
    }

  private List<Map<String, Object>> getMap (List<CSVRecord> records, Map<String, Integer> headers) {
        Map<Integer, String> headerMap = formatHeaderMap(headers);
        List<Map<String, Object>> data = new ArrayList<>();
        for (int i = 1; i < records.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            try {
                CSVRecord record = records.get(i);
                for (int j = 0; j < record.size(); j++) {
                    map.put(headerMap.get(j), record.get(j));
                }
                data.add(map);
            } catch (Exception e) {
                throw new Exception(e);
            }
        }
        return data;
    }


 private Map<Integer, String> formatHeaderMap(Map<String, Integer> map) {
        Map<Integer, String> data = new HashMap<>();
        map.forEach((k , v) -> data.put(v, inputSanitizerForUtf8(k)));
        return data;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.