PapaParse icon indicating copy to clipboard operation
PapaParse copied to clipboard

Add the ability to define columns when the file has no header

Open drmencos opened this issue 3 years ago • 2 comments

As a workaround I'm having to parse, unparse and parse again in order to read a file with no headers and get an array of objects:

Papa.parse(file, {
    complete: result => {
        let contentWithHeader = Papa.unparse({
            fields: ['column1', 'column2', 'column3'],
            data: result.data
        });
        let newResult = Papa.parse(contentWithHeader, {
            header: true
        });

        console.log(newResult);
    }
});

File:

valueA;valueB;valueC
valueD;valueE;valueF
...

Result:

data: [{
    column1: "valueA"
    column2: "valueB"
    column3: "valueC"
}, {
    column1: "valueD"
    column2: "valueE"
    column3: "valueF"
}, ...
]

I could also append the headers to the file, but I'd have to read it first and I don't think that would be good for performance neither.

drmencos avatar Aug 12 '22 18:08 drmencos

Makes sense for me. It iwll be great if you can fill a PR implementing it

pokoli avatar Aug 16 '22 07:08 pokoli

a 'fields' configuration option may receive an array of column names that would override the first line originating headers, if they exist.

alexbodn avatar May 12 '23 12:05 alexbodn