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:

first, second
138901801, 849043027
389023890, 382903205
749029820, 317891093
...

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?

const parsedData = [];

for (const row of file) {
  parsedData.push(row[0].toString() + row[1].toString());
}

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:

const {readFile} = require("fs");

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

readFile("myFile.txt", "utf8", (error, textContent) => {
  if(error){ throw error; }
  const parsedData = [];
  for(let row of textContent.split("n")){
    const rowItems = row.split(",");
    parsedData.push(rowItems[0].toString() + rowItems[1].toString());
  }
}

(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:

const textContent = String.fromCharCode.apply(null, new Uint16Array(buffer));

…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