Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I append one line to the last end in out.csv? #25

Closed
luckymore opened this issue May 16, 2019 · 9 comments
Closed

How can I append one line to the last end in out.csv? #25

luckymore opened this issue May 16, 2019 · 9 comments

Comments

@luckymore
Copy link

This will overwrite the existing file data

const createCsvWriter = require('csv-writer').createObjectCsvWriter;  
const csvWriter = createCsvWriter({  
  path: 'out.csv',
  header: [
    {id: 'name', title: 'Name'},
    {id: 'surname', title: 'Surname'},
    {id: 'age', title: 'Age'},
    {id: 'gender', title: 'Gender'},
  ]
});

const data = [  
  {
    name: 'John',
    surname: 'Snow',
    age: 26,
    gender: 'M'
  }, {
    name: 'Clair',
    surname: 'White',
    age: 33,
    gender: 'F',
  }, {
    name: 'Fancy',
    surname: 'Brown',
    age: 78,
    gender: 'F'
  }
];

csvWriter  
  .writeRecords(data)
  .then(()=> console.log('The CSV file was written successfully'));
@ryu1kn
Copy link
Owner

ryu1kn commented May 16, 2019

Hi @luckymore , you can specify append: true when you create csvWriter.

https://github.com/ryu1kn/csv-writer#parameters

@luckymore
Copy link
Author

Thank god!!! you have created the append option! module fast-csv don't have this...

@luckymore
Copy link
Author

@ryu1kn I have another question. how big is too big for the csv file handling with nodejs

@ryu1kn
Copy link
Owner

ryu1kn commented May 16, 2019

I assume you're passing huge amount of records to a single writeRecords call. It depends on your memory size.

If you split it into multiple writeRecords calls, you wouldn't have the memory problem. Or you can create your transform stream, see this comment.

@luckymore
Copy link
Author

luckymore commented May 16, 2019

@ryu1kn oh~ stream and pipe can hold everything. When set append: true, will need read the whole file before write file. I want know the theory.

@ryu1kn
Copy link
Owner

ryu1kn commented May 16, 2019

You use append: true if the csv file you want to write to already exist and have records in it.

Once you created csvWriter, this is tied to your csv file; so you just call csvWriter.writeRecords whenever you want to add more records into the csv file. If you're getting data by chunks, you would call writeRecords multiple times I guess.

@luckymore
Copy link
Author

yeah, you know what I think

@kanishkamehta801
Copy link

but when append is set to true, headers are not showing

@ryu1kn
Copy link
Owner

ryu1kn commented Jul 1, 2022

@kanishkamehta801 You use append: true when the file you want to write CSV data to already exists (so headers are already in the file).

If you're create a new file, then you don't use append: true. CSV Writer will create a file with a header record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants