NodeJS - Edit a csv cell
Clash Royale CLAN TAG#URR8PPP
NodeJS - Edit a csv cell
I want to modify some cells in a CSV file and first read the file and get the data of the part I want. However there is a problem when it saves in that it flattens the rows and also turns the ;
characters into ,
also adding an extra comma to the line I modify.
;
,
How can I modify and save a certain cell's data?
var fs = require('fs');
var parse = require('csv-parse');
var parser = parse(delimiter: ';', function(err, data)
var row0 = '' + data[0];
var index = row0.split(',')[0];
data[0] = index + ';' + 'Modified Cell Here' + ',' + row0.split(',')[2];
fs.writeFile("./Sample.csv", data, function(err) if(err) return console.log(err); );
console.log(data);
);
fs.createReadStream(__dirname+'/Sample.csv').pipe(parser);
The Sample.csv is:
0;123,abc
1;456,efg
2;789,hij
In the modified Sample.csv it returns as:
0;Modified Cell Here,abc,1,456,efg,2,789,hij
I was expecting this:
0;Modified Cell Here,abc
1;456,efg
2;789,hij
1 Answer
1
I suspect you could join the array back with a newline before writing
snip::
data[0] = index + ';' + 'Modified Cell Here' + ',' + row0.split(',')[2];
var outputData = data.join('n') ;
fs.writeFile("./Sample.csv", outputData, function(err) if(err) return console.log(err); );
::snip
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This seems to work well thank you
– Bluedragon
Aug 10 at 18:10