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

422
Visualizações
How do I run the data in Chunks for Papa Parse?

My file size is 2483385kb, the following code using Papa Parser does not help me:

 this.readFile((output) => {
                _this.sample = get(Papa.parse(output, {preview: 2, skipEmptyLines: true, chunkSize: 1024 * 1024 * 45}), "data");
                _this.csv = get(Papa.parse(output, {skipEmptyLines: true, chunkSize: 1024 * 1024 * 45}), "data");
            });

It cannot read the large csv file. Am I doing it wrongly?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can try streaming.

/**
 * Returns an async generator that yields the CSV file line-by-line.
 *
 * @param path {String}
 * @param options {papa.ParseConfig}
 */
async function* readCsvStream(path, options) {
  const csvStream = createReadStream(path);
  const parseStream = papa.parse(papa.NODE_STREAM_INPUT, options);
  csvStream.pipe(parseStream);
  for await (const chunk of parseStream) {
    yield chunk;
  }
}

(async () => {
  try {
    const asyncIterator = readCsvStream(`${__dirname}/cad_fi.csv`, {
      header: true,
      dynamicTyping: true,
    });

    for await (const chunk of asyncIterator) {
      // You can do anything with each row of the `.csv` file here.
      // like saving it to a DB row by row
      console.log(chunk);
    }
  } catch (error) {
    console.error(error);
  }
})();

Or, alternatively, you can pass a step callback to the parse function, which will be called on each line, thus avoiding loading the entire csv in memory.

/**
 * Reads a CSV file.
 *
 * @param path {String}
 * @param options {papa.ParseConfig}
 */
function readCsv(csvString, stepCallback, options) {
  papa.parse(csvString, {
    ...options,
    step: stepCallback,
  });
}

(async () => {
  // I read it from FS because it is NodeJs, but you can get the string by any means.
  const csv = (await readFile(`${__dirname}/cad_fi.csv`)).toString();

  const data = [];
  const errors = [];
  const stepCallback = (results, parser) => {
    if (results?.errors?.length) {
      errors.push(...results.errors);
    }
    data.push(results.data);
  };

  const papaparseOptions = {
    header: true,
    dynamicTyping: true,
  };

  try {
    readCsv(
      csv,
      stepCallback,
      papaparseOptions,
    );
    console.log(errors);
    console.log(data.length);
  } catch (error) {
    console.error(error);
  }
})();

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