admin管理员组

文章数量:1295909

I'm trying to loop through a string of url images then grab the 3 rgb value colors which splashy.js does, then using csv-writer to display the data in csv file, but unfortunately instead of adding to the file csv-writer keeps rewriting the file rather then adding to the file. How to fix this?

I'm trying to loop through a string of url images then grab the 3 rgb value colors which splashy.js does, then using csv-writer to display the data in csv file, but unfortunately instead of adding to the file csv-writer keeps rewriting the file rather then adding to the file. How to fix this?

Share Improve this question edited Dec 11, 2018 at 6:05 Mateen Kazia asked Dec 11, 2018 at 5:00 Mateen KaziaMateen Kazia 3491 gold badge4 silver badges19 bronze badges 3
  • 3 Please post your code, not an image of your code – Jack Bashford Commented Dec 11, 2018 at 5:13
  • Just updated the post sorry for that – Mateen Kazia Commented Dec 11, 2018 at 5:44
  • You should post the code without your fixes ;) – aadlani Commented Dec 11, 2018 at 5:59
Add a ment  | 

3 Answers 3

Reset to default 3

On the first line, csv-writer#createobjectcsvwriter accepts an optional parameter to specify that you want to append to the file instead of overwriting it. You can find the details on the npm package description

createObjectCsvWriter(params) Parameters:

params <Object>
   - append <boolean> (optional)
     Default: false. When true, it will append CSV records to the specified file. If the file doesn't exist, it will create one.

Your line should read something like:

const createCsvWriter = require("csvwriter").createObjectCsvWriter({ append: true }) 

This can also be achieved without depending on any 3rd party npm module . We can generate csv files by use core node.js functionalities.

For that case I need to understand in what format you are looking for the csv file.

A basic csv file can be generated as :

let headers = ["h1","h2","h3","h4"].join("\t");
let row1    = ["r1","r2","r3","r4"].join("\t");
let row2    = ["t1","t2","t3","t4"].join("\t");

let writeStream = headers+"\n"+row1+"\n"+row2+"\n";

let fs = require("fs");

fs.writeFile("file.csv", writeStream)
.then(file => {{file details}})
.catch(err => err)

If you just add append: true to your createCsvWriter call argument, it would somewhat work, though the order of CSV records depends on the order splashy.fromUrl finishes its work. Also, because you use append mode, you won't get the header record in the output CSV.

Instead, you can define csvWriter outside of your imgs loop.

const csvWriter = createCsvWriter({
  path: "./data/output.csv",
  header: [
    { id: "url", title: "URl" },
    { id: "color1", title: "Color" },
    { id: "color2", title: "Color" },
    { id: "color3", title: "Color" }
  ]
});

const promiseForRecords = imgs.map(async img => {
  const colors = await splashy.fromUrl(img.toString());
  return {
    url: img,
    color1: colors[0],
    color2: colors[1],
    color3: colors[2]
  };
});

Promise.all(promiseForRecords)
  .then(records => csvWriter.writeRecords(records))

This way, the record order is guaranteed and you'll get the header record in the first line.

The advantage of using csv-writer library here is that you don't need to worry whether any image URLs contain ma , or double-quotes " which need to be escaped in CSV.

If you do want to write CSV line-by-line, you can use .reduce instead of .map like this:

imgs.reduce(async (promise, img) => {
  await promise;

  const colors = await splashy.fromUrl(img.toString());
  const records = [
    {
      url: img,
      color1: colors[0],
      color2: colors[1],
      color3: colors[2]
    }
  ];
  return csvWriter.writeRecords(records);
}, Promise.resolve());

本文标签: javascriptWrite result into csv file in nodeJSStack Overflow