Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
Reading csv with fast-csv in node.js when 3 columns have same captions

I am trying to read a CSV file with fast-csv in Node.js. The csv file has a header row which contains 90 columns. 87 of them have unique names. But 3 columns have the same header name "Transformation". This is obviously a semantic mistake. Fast-csv is complaining that headers are not unique and does not read the CSV. I know, all column names should be different, but this is how I get the CSV file. Is there a way to read the headers and if some column has a name that another column has, the name automatically converts to "Transformation_int", with int being some random number?

csv.parseFile(csvFileName,
       { headers: true, delimiter: ";", 
         headers: headers => headers.map(
          h => {h.replace( /[\r\n]+/gm, " " )}) 
        })
    .on ("data", function (row) { <doing something> })
    .on ("error", error => {console.log("This error occured:",error)})
    .on ("end", () => {      console.log("Import finished")});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you could not use headers at all, and then the first row will contain headers, and you can manually process the rest of the data with reference to the first row as you need, and even keep duplicate names:

headers: false

or, you can pass a transform function which detects duplicates, and also renames them

here's a quick example which renames duplicate headers by adding random number:

csv.parseFile(csvFileName, {
        delimiter: ";",
        headers: headers => {

            // rename duplicate
            headers.map((el, i, ar) => {
                if (ar.indexOf(el) !== i) {
                    headers[i] = `${el}_${i}`;
                }
            });

            return headers;
        }
    })
    .on("data", function(row) {
        console.log('row', row);
    })
    .on("error", error => {
        console.log("This error occured:", error)
    })
    .on("end", () => {
        console.log("Import finished")
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda