Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new column to csv file using node js

Tags:

node.js

csv

I already tried this using node.js npm package fast-csv, but I don't get a solution, I can read csv file successfully, now I need to add a new column to my existing csv file.

My questions:

How to add new column to csv file? How to update csv?

var csv = require("fast-csv");
var fs = require('fs');
var stream = fs.createReadStream("file1.csv");
var service = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=53.78943,-0.9985&destinations=53.540867,-0.510699&mode=driving&language=en-US';
var source = [];
var dest = [];
var distance = require('google-distance');

distance.apiKey = '************';
var i = 1;
csv
    .fromStream(stream, { headers: true })
    .on("data", function(data) {
        //get source and distance array
        source = data.SourceLatLong;
       dest = data.DestBREPLatLong;
        //print source and destinatoon
        console.log(source);
        console.log(dest);
        distance.get({
                // index: i,
                origin: source,
                destination: dest,
                units: 'imperial'
            },

            function(err, map_data) {

                if (err) return console.log(err);
                //console.log(map_data);
                //console miles of aff
                console.log('source lat long ' + ':' + data.SourceLatLong + ' , ' + 'Dest lat long' + ':' + data.DestBREPLatLong + ',' + ' distance ' + ':' + map_data.distance + ' ' + i++);

            });
    })

.on("end", function() {
    console.log("done");
});

In the above program I use the filecsv file1.csv ,from i take two columns SourceLatLong and DestLatLong and I calculate distance in miles. Now I need to add new miles columns to my file .csv

like image 265
Bhagvat Lande Avatar asked Oct 21 '16 09:10

Bhagvat Lande


People also ask

How do you write data in a CSV file using node JS?

First, we import the native file system module ( fs ) and the csv-parse module. Then, we create a parser which accepts an object literal, containing the options we'd like to set. The second argument is the callback function that's used to access the records - or just print them out, in our case.

How do I make two columns into a CSV file?

csv file is a text file; you can add lines (rows) to an existing file. To add columns you need to write a whole new file. That means load the file into Python object (dataframe), make changes there, and then write the new file. Please provide a sample file/data to give you working code and avoid posting images of data.


1 Answers

Use csv-parser package instead of fast-csv, and json2csv for json to csv parsing.

What you want to do is to convert each row of the csv to json using csv-parser, add a new field into the resulting json object, do this for each row, put the result in an array, convert that array into csv with json2csv package.

Here is the code:

var csv = require('csv-parser');
var fs = require('fs');
var json2csv = require('json2csv');
var dataArray = [];

fs.createReadStream('your-original-csv-file.csv')
  .pipe(csv())
  .on('data', function (data) {
    data.newColumn = newColumnValue;
    dataArray.push(data);
  })
  .on('end', function(){
    var result = json2csv({ data: dataArray, fields: Object.keys(dataArray[0]) });
    fs.writeFileSync(fileName, result);
  });

fs.writeFileSync overrides the original file anyway, so you can also save into the original csv file.

like image 156
ardilgulez Avatar answered Sep 20 '22 20:09

ardilgulez