PapaParse: CSV parsing is broken in 5.4.1

Hi everyone,

First of all, big thanks for this great library that’s been a big help in my work 🙌

I noticed when going from 5.3.2 to 5.4.1 that some CSV files can’t be parsed successfully anymore. I’ll attach below a small CSV, a nodejs script to reproduce the issue as well as the output.

example.csv:

colName_2,colName_3,another_col,entity,some_bool,period,date,value_eur
"foo, bar","foo, bar",foo_bar_baz_V1,bar baz,0,2022_02,02/01/22,1234.45678
"foo, bar","foo, bar",foo_bar_baz_V1,bar baz,0,2022_03,03/01/22,1234.45678
"foo, bar","foo, bar",foo_bar_baz_V2,bar baz,0,2022_11,11/01/22,-12345.67891
"foo, bar","foo, bar",foo_bar_baz_V2,bar baz,0,2022_12,12/01/22,-12345.67891

script.js:

const papa = require('papaparse');
const fs = require('fs');

const readStream = fs.createReadStream(`${ __dirname }/example.csv`);
return papa.parse(readStream, {
    step: (res, parser) => {
        parser.pause();
        Object.keys(res.data).
            sort().
            forEach((key) => {
                console.log(res.data[key]);
            });
        return parser.resume();
    }, // step
    header: true,
    error: (err) => {
        return console.error(err);
    }, // error
    complete: () => {
        console.log('finished');
    }, // complete
}); // return papa.parse

Output with papaparse 5.4.1:

$ node script.js 
foo_bar_baz_V1
foo, bar
foo, bar
02/01/22
bar baz
2022_02
0
1234.45678
[ '-12345.67891' ]
foo, bar
foo, bar
foo_1, bar"_1,foo_bar_baz_V1,bar baz,0,2022_03,03/01/22,1234.45678
"foo, bar
2022_11
foo_bar_baz_V2
0
bar baz
11/01/22
foo, bar"_1,foo_bar_baz_V2,bar baz,0,2022_12,12/01/22,-12345.67891


 bar"
finished

Let me know if you need any additional information to better capture the issue.

Thanks a bunch.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 4
  • Comments: 28

Commits related to this issue

Most upvoted comments

Follow-up – downgrading to papaparse 5.3.2 corrected the problem.

I’m experiencing exactly what @ulo described above. Streaming on 5.4.1 with header: true, the first 16 rows come back fine. The 17th row and beyond are parsed incorrectly.

Converting the stream to a string and parsing that works as expected.

I can repro. this on a very simple implementation.

Emit a dataset like this from a stream.

Foo 1,Foo 2
123,124
124,125
125,126
126,127
127,128
128,129
129,130
130,131
131,132
132,133
133,134
134,135
135,136
136,137
137,138
138,139
139,140
137,137

Details don’t matter, it just needs a unique header and more than 16 rows, and the next row should have a duplicate header to see the bad parsing behavior.

const fileStream = createReadStream(TEST_LONG_CSV_PATH, {
  encoding: "utf8",
});
const streamingParse = csvParser.parse(NODE_STREAM_INPUT, {
  header: true,
});
fileStream.pipe(streamingParse);

Then consume the stream…

for await (const record of streamingParse) {
  console.log(record);
}

Or

streamingParse.on("data", (record: Record<string, string>) => {
  console.log("Parsed record:", record);
})

At row 16 or 17, you’ll get a warning on stderr that says “Duplicate headers found and renamed.” indicating it has seemingly forgotten the headers it parsed at the beginning of the stream. Then it treats the next row like a new set of headers.

The _1 value strikes me as similar to data issues found in #985.

The symptoms are:

  • some values from the CSV are parsed as arrays containing a single value
  • some values get _1 appended to them for no apparent reason
  • the CSV files is not parsed correctly as you can see in the output where multiple values get concatenated together

Maybe to illustrate the point better, here’s the output from the same script but using papaparse 5.3.2 :

$ node script.js 
foo_bar_baz_V1
foo, bar
foo, bar
02/01/22
bar baz
2022_02
0
1234.45678
foo_bar_baz_V1
foo, bar
foo, bar
03/01/22
bar baz
2022_03
0
1234.45678
foo_bar_baz_V2
foo, bar
foo, bar
11/01/22
bar baz
2022_11
0
-12345.67891
foo_bar_baz_V2
foo, bar
foo, bar
12/01/22
bar baz
2022_12
0
-12345.67891
finished

@jbool24 Yeah I don’t have a use for it and would in general not want to use it. I would not be sad if it was removed. But I don’t know anything about whatever use cases resulted it in being added so I’m trying to be open minded about it. I wouldn’t be sad if it was just ripped out. But if it is to stay in I think it should be an option that defaults to ‘off’.

Streaming on 5.5.1 with header: true. Getting the same wrong values _1 … _N.

This is because you have duplicated Headers. Is a new feature introduced in the newer version.

Having the same issue. step works fine if I don’t pause the parser, but rows are not parsed properly with parser.pause().

Run the code below with parser.pause and parser.resume commented out, and not commented out, to compare the output.

import fs from 'fs';
import Papa from 'papaparse';

const fileName = String.raw`Theraphosidae 20241116.csv`

const fileStream = fs.createReadStream(fileName);
Papa.parse(fileStream, {
  header: true, // Convert rows to objects using the first row as header
  step: (results, parser) => {
    parser.pause()
    const row = results.data
    console.log(row.vmNumber)
    parser.resume()
  },
  complete: () => {
    console.log('all done')
  },
  error: (error) => console.log(error),
});

Theraphosidae 20241116.csv

Csv parsing still broken on NodeJS, using v5.4.1 with header: true. Wrong number of rows and invalid row objects filled with “_1”, “_2”, etc… and concatenated columns that should be distinct values. The csv file used is valid and parsed correctly using different csv tools.

EDIT: The bug appears when using the stream mode like this:

import { NODE_STREAM_INPUT, parse } from 'papaparse';

// Some read stream returned from fs.createdReadStream
const csvFileReadStream = ...
csvFileReadStream.pipe(parse(NODE_STREAM_INPUT, options))

Using

parse(csvFileReadStream, options)

looks to work fine (though Typescript complains on the 1st arg of the parse function but that’s maybe some misconfiguration on our side).