node-csv-parse icon indicating copy to clipboard operation
node-csv-parse copied to clipboard

skip_empty_lines property is not working as expected

Open hemanthreddyk opened this issue 4 years ago • 5 comments

skip_empty_lines : Don't generate records for empty lines (line matching /\s*/), defaults to false.

As per the above definition it should ignore a row if it contains any string matching the regex '/\s*/'. But when a row contains a space or tab in it, its not skipping the row and I am encountering following error CsvError: Invalid Record Length: expect 2, got 1 on line 2

As a work around I have set trim as true and then it works. Am I doing something wrong here or is it an issue?

const parse = require('csv-parse')
const assert = require('assert')

parse(`
"key_1","key_2"\n\t\n"value 1","value 2"
`.trim(), {
  // trim: true,
  columns: false,
  skip_empty_lines: true
}, (err, records) => {
  if (err) {
    console.log('******', err)
  }
  assert.deepStrictEqual(
    records, [
         [
         'key_1',
         'key_2'
        ],
        [
         'value 1',
          'value 2'
        ]]
  )
})

hemanthreddyk avatar Feb 09 '21 14:02 hemanthreddyk

Could you edit you issue with a valid markdown syntax using triple backticks, thank you

wdavidw avatar Feb 09 '21 14:02 wdavidw

The doc is wrong. In the current implementation, by "empty", we really mean empty, no spaces, no tabs. Let me see if I can do sth about it

wdavidw avatar Feb 09 '21 14:02 wdavidw

You can combine skip_lines_with_empty_values and relax_column_count. Would that works for you ? I'll fix the doc

wdavidw avatar Feb 09 '21 14:02 wdavidw

My use-case here is to ignore the second row which had tab space in it. By enabling 'relax_column_count' I wont get an error if column count doesn't match with header count which is not desirable for us.

hemanthreddyk avatar Feb 09 '21 15:02 hemanthreddyk

Then your solution with trim shall be appropriate.

wdavidw avatar Feb 09 '21 15:02 wdavidw