Skip to content
Advertisement

Iterate over cells in a CSV file in Node.js

I have a CSV file: “myCSV.csv” with two columns: “first” and “second”.

All the data inside is just numbers. So the file looks like this:

JavaScript

I would like to iterate over these numbers and perform some custom parsing on them, then store results in an array.

How can I achieve a behavior like the following?

JavaScript

Advertisement

Answer

If you’re working with a file the user has selected in the browser, you’d make a FileReader in response to the user’s action. (See FileReader – MDN.)


But it sounds like you already have the file on your server, in which case you’d use Node’s built-in File System module. (See File System – NodeJS.)

If you just want the module’s readFile function, you’d require it in your file like:

JavaScript

And you’d use it to process a text file like:

JavaScript

(See Node.js – Eloquent JavaScript).


However, if you want to handle your CSV directly as binary data (rather than converting to a text file before reading), you’d need to add something like this before invoking readFile:

JavaScript

…with the textContent parameter in the arrow function replaced by a buffer parameter to handle the binary data.
(If Uint16Array is the wrong size, it might be Uint8Array instead. See Buffer to String – Google.)


You might also find these resources helpful:
JS CSV Tutorial – SeegateSite
JS read-text demo – GeeksForGeeks

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement