1

Have tried PapaParse without success, how would one get the first column value of a CSV file?

const csv = require('csv-parser');
const fs = require('fs');


(async () => {
    try {

        fs.createReadStream('test.csv')
            .pipe(csv())
            .on('data', (row) => {
                console.log(row);
            })
            .on('end', () => {
                console.log('CSV file successfully processed');

            });

    } catch (err) {
        console.log(error(err));
        await browser.close();
        console.log(error("Browser Closed"));
    }
})();
1
  • You need to create an accumulator (eg: an empty array) before creating the read stream. Then, in the on('data') callback, you would push the first item in the row into the accumulator. Finally, in the on('end') callback, you resolve the promise with the value in the accumulator. Of course, you need to wrap your logic in a promise callback to begin with. Commented Sep 27, 2019 at 20:56

1 Answer 1

2

For anyone in the future, set a function then set as a const to your CSV list of URLs, the number [1] represents the column.

function readURLFile(path) {
    return fs.readFileSync(path, 'utf-8')
        .split('\n')
        .map((elt) => {
            const url = elt.split(',')[1].replace('\r', '');
            return `http://${url.toLowerCase()}`;
        });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.