Remove Columns from a . csv file with Javascript

Asked

Viewed 198 times

0

Below follows my code for reading a file. csv using Nodejs, I would like to know how I can remove the "COMPANY" and "%" columns from the file 'file.csv', thanks in advance to those who contribute.

var fs = require('fs')
var csv = require('fast-csv')
var list = []

fs.createReadStream('file.csv')
  .pipe(csv())
  .on('data', data => {

    list = data;

    for (let i = 0; i < list.length; i++) {
      console.log(list[i])
    }
  })
  .on('end', function (data) {
    console.log('\nleitura finalizada!')
  })

File.csv link used in code reference: Download the.csv file

Used the fast-csv documentation link library: Documentation of fast-csv

1 answer

0


I made the following changes in the section ". on({ changed-here })", being necessary the creation of the variable output. this solved my problem regarding the omission of the columns "COMPANY" and "%".

//Declarações de variaveis globais:
let output = ''

  //Trecho alterado:
  .on('data', data => {

    if (list[i] == null) {
      list[i] = data.CREDENCIADO + ';' + data.USUARIO.substring(2) + ';' + data['AUTORIZACAO'] + ';' + data['DATA AUT'] + ';' + data['DATA VEN'] + ';' + data.PARCELA + ';' + data['TOTAL PARCELAS'] + ';' + 'R$ ' + data.VALOR.replace('.', ',').toLocaleString('pt-br', {
        style: 'currency',
        currency: 'BRL'
      }) + '\n'

      if (output == '') {
        output = list[i]
        console.log(list[i])
        i++
      } else {
        output += list[i]
        console.log(list[i])
        i++
      }
    }
  })

Example Code on Github: FORMATTER-CSV-TO-XLSX-NODE-JAVASCRIPT

Browser other questions tagged

You are not signed in. Login or sign up in order to post.