Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append to a csv file in nodejs (using csv-write-stream)

I am currently using csv-write-stream to write something to a csv file:

var fs = require('fs');
var csvWriter = require('csv-write-stream')

var writer = csvWriter()
writer.pipe(fs.createWriteStream('out.csv'))
writer.write({hello: "world", foo: "bar", baz: "taco"})
writer.end()

I like how easy this is. However, this always creates a new file. How could I append something to this file? Could I even use the same library?

like image 894
Peter Zacharias Avatar asked Dec 03 '16 14:12

Peter Zacharias


3 Answers

Actually, fs.createWriteStream function decides how to open 'out.csv'

In your case, you can open this file for adding by using a flag:

writer.pipe(fs.createWriteStream('out.csv', {flags: 'a'}))

Here are the docs for this function and flags.

like image 189
idmitme Avatar answered Nov 15 '22 23:11

idmitme


Maybe late, but for people who encounter the same problem as me Error: no headers specified

  if (!fs.existsSync(finalPathFile))
    writer = csvWriter({ headers: ["header1", "header2"]});
  else
    writer = csvWriter({sendHeaders: false});

  writer.pipe(fs.createWriteStream(finalPathFile, {flags: 'a'}));
  writer.write({
    header1:"hello",
    header2:"world",
  });
  writer.end();

The concept stands for when you first access the file, you set the headers, so it can later be readable by other backend software like Excel. After that, when you later access the file again, you do not set the headers. But take in mind, each time you write to the file, you need to set the which column you're writing to. Then you'll get your desired result, which stands something like this:

header1,header2
John,5th avenue,
Richard,St. Jorge street
like image 43
James Avatar answered Nov 15 '22 22:11

James


I was having issues where the first row of data I tried to write after the header would be empty (just a bunch of commas like this ,,,,,).

I also was getting the Error: No headers message when I tried to set sendHeaders false.

My solution was based original @Firecat answer. I modified to this which works really well for me.

const fs = require('fs');
var csvWriter = require('csv-write-stream');
var writer = csvWriter({sendHeaders: false}); //Instantiate var
var csvFilename = "C:\some\path\myfile.csv";

// If CSV file does not exist, create it and add the headers
if (!fs.existsSync(csvFilename)) {
  writer = csvWriter({sendHeaders: false});
  writer.pipe(fs.createWriteStream(csvFilename));
  writer.write({
    header1: 'DATE',
    header2: 'LASTNAME',
    header3: 'FIRSTNAME'
  });
  writer.end();
} 

// Append some data to CSV the file    
writer = csvWriter({sendHeaders: false});
writer.pipe(fs.createWriteStream(csvFilename, {flags: 'a'}));
writer.write({
  header1: '2018-12-31',
  header2: 'Smith',
  header3: 'John'
});
writer.end();

// Append more data to CSV the file    
writer = csvWriter({sendHeaders: false});
writer.pipe(fs.createWriteStream(csvFilename, {flags: 'a'}));
writer.write({
  header1: '2019-01-01',
  header2: 'Jones',
  header3: 'Bob'
});
writer.end();
like image 42
jsherk Avatar answered Nov 15 '22 22:11

jsherk