Node.js Generated Csv File Is Displaying £ For A Uk Pound Sign (£)
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
Solution 1:
I have got the solution from this answer and it's question thread: https://stackoverflow.com/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.
Post a Comment for "Node.js Generated Csv File Is Displaying £ For A Uk Pound Sign (£)"