Hell stackoverflow. I am wondering how to process some data from a text document. Let us just say I have a document that stores information about reptiles like this.
reptiles.txt
name:age:length:color
name:age:length:color
name:age:length:color
I want a node script to read that file and store all of that info into a object variable like this.
reptile[name:age:length:color]
reptile[name:age:length:color]
reptile[name:age:length:color]
Thank you very much.
Here is the updated code.
const fs = require('fs')
var CSV = {
  parse: function(csv, reviver) {
      reviver = reviver || function(r, c, v) { return v; };
      var chars = csv.split(''), c = 0, cc = chars.length, start, end, table = [], row;
      while (c < cc) {
          table.push(row = []);
          while (c < cc && '\r' !== chars[c] && '\n' !== chars[c]) {
              start = end = c;
              if ('"' === chars[c]){
                  start = end = ++c;
                  while (c < cc) {
                      if ('"' === chars[c]) {
                          if ('"' !== chars[c+1]) { break; }
                          else { chars[++c] = ''; } // unescape ""
                      }
                      end = ++c;
                  }
                  if ('"' === chars[c]) { ++c; }
                  while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ':' !== chars[c]) { ++c; }
              } else {
                  while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ':' !== chars[c]) { end = ++c; }
              }
              row.push(reviver(table.length-1, row.length, chars.slice(start, end).join('')));
              if (':' === chars[c]) { ++c; }
          }
          if ('\r' === chars[c]) { ++c; }
          if ('\n' === chars[c]) { ++c; }
      }
      return table;
  },
  stringify: function(table, replacer) {
      replacer = replacer || function(r, c, v) { return v; };
      var csv = '', c, cc, r, rr = table.length, cell;
      for (r = 0; r < rr; ++r) {
          if (r) { csv += '\r\n'; }
          for (c = 0, cc = table[r].length; c < cc; ++c) {
              if (c) { csv += ':'; }
              cell = replacer(r, c, table[r][c]);
              if (/[:\r\n"]/.test(cell)) { cell = '"' + cell.replace(/"/g, '""') + '"'; }
              csv += (cell || 0 === cell) ? cell : '';
          }
      }
      return csv;
  }
};
//
const reptiles = fs.readFileSync('./text.txt', 'utf8')
//
for (let reptile of reptiles) {
  [name, age, length, color] = reptile
  console.log(`${name} is ${age} old, is ${length} long and is a ${color} colour`)
}
console.log((reptiles))
The output of this is this.
T is undefined old, is undefined long and is a undefined colour
u is undefined old, is undefined long and is a undefined colour
r is undefined old, is undefined long and is a undefined colour
t is undefined old, is undefined long and is a undefined colour
l is undefined old, is undefined long and is a undefined colour
...
This is how I did it.
let text = fs.readFileSync('./text.txt', 'utf8');
const reptiles = CSV.parse(text)
Thank you friends!
 
     
     
    