javascript - Node.js generated csv file is displaying £ for a UK pound sign (£) - Stack Overflow

I'm using this json2scv package parsing my data (sample json data is described in below code)I am

I'm using this json2scv package parsing my data (sample json data is described in below code)

I am trying to generate a CSV file in my nodejs application using the code below:

If I open the file in Excel, then I get £ wherever a £ sign should appear.

var json2csv = require('json2csv');
var fs = require('fs');
var fields = ['car', 'price', 'color'];
var myCars = [
  {
    "car": "Audi",
    "price": "£40000",
    "color": "blue"
  }, {
    "car": "BMW",
    "price": "£35000",
    "color": "black"
  }, {
    "car": "Porsche",
    "price": "£60000",
    "color": "green"
  }
];
var csvStr = json2csv({ data: myCars, fields: fields, del: ',' });

fs.writeFile('file.csv', csvStr, { encoding: 'utf8' },function(err) {
  if (err) throw err;
  console.log('file saved');
});

Any Thoughts ?

Thanks.

I'm using this json2scv package parsing my data (sample json data is described in below code)

I am trying to generate a CSV file in my nodejs application using the code below:

If I open the file in Excel, then I get £ wherever a £ sign should appear.

var json2csv = require('json2csv');
var fs = require('fs');
var fields = ['car', 'price', 'color'];
var myCars = [
  {
    "car": "Audi",
    "price": "£40000",
    "color": "blue"
  }, {
    "car": "BMW",
    "price": "£35000",
    "color": "black"
  }, {
    "car": "Porsche",
    "price": "£60000",
    "color": "green"
  }
];
var csvStr = json2csv({ data: myCars, fields: fields, del: ',' });

fs.writeFile('file.csv', csvStr, { encoding: 'utf8' },function(err) {
  if (err) throw err;
  console.log('file saved');
});

Any Thoughts ?

Thanks.

Share Improve this question asked Jul 21, 2017 at 8:12 narainsagarnarainsagar 1,1292 gold badges13 silver badges29 bronze badges 2
  • 1 You have a text encoding issue. You're saving UTF-8 but at some point this is being read as ISO-8859-1 (or similar). Try adding a BOM at the start of the file, saving in 8859-1, or explicitly specifying the encoding on loading. – Richard Commented Jul 21, 2017 at 8:16
  • @Richard adding BOM "\ufeff" and using encoding "utf16le" solved the issue. Thanks for your feedback thou. – narainsagar Commented Jul 21, 2017 at 8:26
Add a ment  | 

2 Answers 2

Reset to default 5

I have got the solution from this answer and it's question thread: https://stackoverflow./a/27975629/5228251

UTF-8 Example:

fs.writeFile(someFilename, '\ufeff' + html, { encoding: 'utf8' }, function(err) {
   /* The actual byte order mark written to the file is EF BB BF */
}

UTF-16 Little Endian Example:

fs.writeFile(someFilename, '\ufeff' + html, { encoding: 'utf16le' }, function(err) {
   /* The actual byte order mark written to the file is FF FE */
}

After go through above answer and it's thread then I have modified my code like this:

  • Changed delimeter option to "\t" instead of ","
  • Prepended "\ufeff" to the csv string
  • Changed encoding to use "utf16le" instead of "utf8"

Here's my updated code:

var json2csv = require('json2csv');
var fs = require('fs');
var fields = ['car', 'price', 'color'];
var myCars = [
  {
    "car": "Audi",
    "price": "£40000",
    "color": "blue"
  }, {
    "car": "BMW",
    "price": "£35000",
    "color": "black"
  }, {
    "car": "Porsche",
    "price": "£60000",
    "color": "green"
  }
];
var csvStr = json2csv({ data: myCars, fields: fields, del: '\t' });

fs.writeFile('file.csv', '\ufeff' + csvStr, { encoding: 'utf16le' },function(err) {
  if (err) throw err;
  console.log('file saved');
});

Hope this helps others.

in 2022 answer

(using changed), call Parser class from json2csv lib

const { Parser } = require('json2csv');

const myCars = [
  {
    "car": "Audi",
    "price": 40000,
    "color": "blue"
  }, {
    "car": "BMW",
    "price": 35000,
    "color": "black"
  }, {
    "car": "Porsche",
    "price": 60000,
    "color": "green"
  }
];

const json2csvParser = new Parser();
const csv = json2csvParser.parse(myCars);

console.log(csv);

With delimiter:

const json2csvParser = new Parser({ delimiter: '\t' });
const tsv = json2csvParser.parse(myCars);

With fields

const fields = ['carModel', 'price', 'colors'];
const json2csvParser = new Parser({ fields, transforms });
const csv = json2csvParser.parse(myCars);

More examples here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742359356a4429080.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信