PapaParse icon indicating copy to clipboard operation
PapaParse copied to clipboard

No newLine at csv end

Open mkaufma opened this issue 5 years ago • 5 comments

https://github.com/mholt/PapaParse/blob/a66776140f25a69cb3469cfdfcfc1f669db1c709/papaparse.js#L431

I think data.length - 1 should be changed to data.length.

Otherwise, when using papaparse to write to a stream in chunks we have to add the newLine ourselves.

mkaufma avatar Jun 03 '19 11:06 mkaufma

It will be great if you can upload a PR with a test case demostrating your scenario.

Thanks!

pokoli avatar Jun 04 '19 17:06 pokoli

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

let csvStream;
let shouldWriteHeadersRow = true;
let reportHeaders = ['a', 'b'];

function initStream(csvPath) {
    try {
        csvStream = fs.createWriteStream(csvPath);
    } catch (err) {
        throw new Error(err);
    }
}

async function closeStream() {
    await csvStream.end();
}

function getParseConfig() {
    if (shouldWriteHeadersRow) {
        shouldWriteHeadersRow = false;
        return { columns: reportHeaders, header: true };
    }
    return { columns: reportHeaders, header: false };
}

async function writeToStream(dataArray) {
    try {
        const parseConfig = getParseConfig();
        const csv = papa.unparse(dataArray, parseConfig);
        await csvStream.write(csv);
    } catch (err) {
        throw new Error(err);
    }
}

async function runTest() {
    initStream('./tst.csv');
    await writeToStream([{ a: '1', b: '2'}, { a: '11', b: '22'}]);
    await writeToStream([{ a: '111', b: '222'}, { a: '1111', b: '2222'}]);
    await closeStream();
}

runTest().then(() => {
    console.log('write finished');
}).catch(e => {
    console.error(e);
});

mkaufma avatar Jun 05 '19 09:06 mkaufma

The above code creates a faulty csv file: instead of:

a,b
1,2
11,22
111,222
1111,2222

we get

a,b
1,2
11,22111,222
1111,2222

mkaufma avatar Jun 05 '19 09:06 mkaufma

If you add a config option to make sure unparse result always ends with 'endLine' it would be great

mkaufma avatar Jun 05 '19 09:06 mkaufma

@mkaufma @pokoli Also having the same issue. I'm using PapaParse for writing multiple times to the same file (e.g. streaming writes).

dino-rodriguez avatar Oct 15 '20 20:10 dino-rodriguez