0

I'm creating a .csv file with nodejs fw-extra. I'd like to open this file using excel, however, I'm having problems where I have comma's in my string. I've been looking on stack overflow for a solution. The recommendation is to replace a, with "". When I do this, the "" appear in excel. It doesn't seem to recognise this pattern. What am I missing?

The string I'm trying to save is bob, 2. In the example below I use the double quotes instead of a , as per the suggestions I've read.

const fs = require("fs-extra");

const name = 'bob"" 2';

async function loadUrl(page, url) {
  await fs.writeFile(
    "out.csv",
    "name\n"
  );
console.log(1);

  await fs.appendFile(
    "out.csv",
    `${name}\n`
  );
}

loadUrl();
bp123
  • 2,756
  • 4
  • 22
  • 53
  • In a CSV any field which contains a comma should be wrapped in "". E.g. `A,"B,C",D` – Tim Williams Aug 21 '19 at 02:58
  • Yes. When I do this and open it in excel the quotation marks are still there. Is this way it is supposed to work? – bp123 Aug 21 '19 at 03:02
  • There are 3 fields there. If I save that exact text in "tmp.csv" then open it in excel I get "A" in A1, "B,C" in B1 and "C" in C1 (without the actual quotes) – Tim Williams Aug 21 '19 at 04:14

1 Answers1

0

i'll let this post talking about the difference between ' and " in JavaScript, what-is-the-difference-between-and-in-javascript. With that being said, maybe your problem would reach a solution if you use ' to create your strings except in the case where you have a double quote inside, in which case you should use "

Torgon
  • 111
  • 8