Write CSV file column title in javascript - javascript

This is my code. I am trying to output my data as CSV and I am being successful but I can't add the title of the columns. Although I saved the CSV file but the column titles are missing. Can anyone help in this?
var d= new Date();
var t=d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
var data = dataAWS
var result = data.map(function(val) {
return t+'","'+val.payload.test_data;
}).join('"\n"');
var csvData = '"'+ result +'"';
var fs = require('fs');
fs.writeFile("tempN.csv", csvData, 'utf8', function (err) {
if (err) {
console.log("An error occured while saving CSV.");
return console.log(err);
}
console.log("CSV file has been saved.");
});

Without knowing more I would suggest something like:
data.unshift(column_headers);
var result = data.map(function(val) {
return t+'","'+val.payload.test_data;
}).join("\n")
etc, etc

Related

Save js variable data in txt

I have the following code and I need to save allProducts in a .txt file with json format. How can I achieve that?
var fruit = [{"name: x","id: y",..}, {...},...]
var driedFruitsNut = [{"name: x","id: y",..}, {...},...]
fruit.forEach((item) =>{
item.category = "Fruits & Vegetables";
item.subtCategory = "Fruit";
});
driedFruitsNut.forEach((item) => {
item.category = "Fruits & Vegetables";
item.subtCategory = "Dried Fruits & Nuts";
});
var allProducts = fruit.concat(driedFruitsNut);
I tried something like this; but I have no idea if this is even a correct code:
var fs = require('fs');
fs.writeFile("test.txt", allProducts, function(err) {
if (err) {
console.log(err);
}
});
but when I run node fetchFruitVeg.js in my terminal. I get a text file looking like this:
You can try something like this:
var fs = require('fs');
fs.writeFile("test.txt", JSON.stringify(allProducts), function(err) {
if (err) {
console.log(err);
}
});
since allProducts is a javascript object, first you need to convert it into normal string data to write it in a .txt file or .json file

Javascript: Parsing HTML table with Cheerio

I am trying to develop a web scraper using Cheerio to parse an HTML table and output the results in a CSV file, unfortunately the code won't return anything. My code is the following:
var request = require("request");
var cheerio = require("cheerio");
var fs = require("fs");
var url = "https://stat.epa.gov.tw/";
request(url, function(err, response, html){
if(!err) {
var $ = cheerio.load(html);
var allitems = $('table.tableAQI').children().children();
var items = []
allitems.each(function(index){
var result = $('table.tableAQI').children().children().eq(index).find("td").text();
if(result !="") {
items.push(result);
}
});
fs.writeFile("output2.csv", JSON.stringify(items, null, 4), function(err){
if(err) {
console.log(err);
} else {
console.log("Data has been written");
}
});
console.log(items);
}
});
The website provides 2 tables I tried the same code to parse the other table ('table.aqicolor') and it worked just fine so I am not really sure what is wrong.
I appreciate your support.

edit JavaScript files using node js

How can I find and edit an attribute value in a JavaScript file using nodeJS.
for example: If i have a file called app.js containing following code.
let title = "hello world";
document.getElementById("arr").innerHTML = title;
how can I change the value of title let title = "hello world"; or any other attribute using nodeJS.
Instead of trying to parse/edit JS files why not use something like JSON which is easy to serialize?
Your gulp task can write whatever values are needed to a file:
const fs = require('fs')
fs.writeFileSync('./blah.json', JSON.stringify({ title: "whatever" }))
Then your code references the JSON file
const title = require('./blah.json').title
document.getElementById("arr").innerHTML = title;
I was able to edit a JavaScript file using regex. my code is below:
function updateFile(filename, replacements) {
return new Promise(function(resolve) {
fs.readFile(filename, 'utf-8', function(err, data) {
var regex, replaceStr;
if (err) {
throw (err);
} else {
regex = new RegExp("(\\" + 'let' + "\\s* ]*" + replacements[0].rule + "\\s*=\\s*)([^\\n;}]+)([\\s*;}])");
replaceStr = "$1" + replacements[0].replacer + "$3";
data = data.replace(regex, replaceStr);
}
fs.writeFile(filename, data, 'utf-8', function(err) {
if (err) {
throw (err);
} else {
resolve();
}
});
});
})
}
usage:
var file = src/app.js
var myNewValue = "Hello";
updateFile(file, [{
rule: 'newApp',
replacer: myNewValue
}], function (err) {
sails.log.info((err));
});
this will edit,
let newApp = lol; to let newApp = Hello;

nodejs: compare function generated JSON data to JSON file

I have a function that scans a directory and creates a JSON file with the audio files metadata. I want it to check if the file already exists and only overwrite if there is any diference between the file that was created from the last time the script was run and the data from the the second time it runs.
This is my code:
var fs = require('fs');
var nodeID3 = require('node-id3');
var path = require('path');
var tracksPath = './public/tracks/';
var dataPath = './public/data/';
fs.readdir(tracksPath,function(err,files){
if(err) {
throw err;
}
//Read the tracks metadata
var tracksMetadata = [];
files.forEach(function(trackName){
var trackFile = nodeID3.read(tracksPath + trackName);
//If the track returns metadata push it to the array
if (trackFile.title && trackFile.artist){
var metadata = {
"filename" : trackName,
"title" : trackFile.title,
"artist" : trackFile.artist
};
tracksMetadata.push(metadata);
}
//If no metadata is found ignore and log it to the console
else if (trackName.charAt(0) != "."){
var filename = {
"filename" : trackName
};
tracksMetadata.push(filename);
console.log(trackName + " doesn't have metadata. Ignoring.");
}
if(fs.existsSync(dataPath + "metadata.json")){
fs.readFile(dataPath + "metadata.json",'utf8', function (err, data){
if (err) throw err;
console.log(JSON.parse(JSON.stringify(data)));
console.log(JSON.parse(JSON.stringify(tracksMetadata)));
console.log(Boolean(JSON.parse(JSON.stringify(data)) == JSON.parse(JSON.stringify(tracksMetadata))));
});
}
});
fs.writeFile(path.join(dataPath, 'metadata.json'),
JSON.stringify(tracksMetadata),'utf8', function(err){
if(err){
throw err;
}
console.log("Tracks Metadata JSON created succesfully");
});
});
Right now I'm only writing to the console a Boolean value that checks wether the data from the file and the data generated by the function are equal and so far I get false.
What should I do?

csvtojson conversion using Nodejs gives gibberish data

I am trying to convert a csv file to json using csvtojson converter in nodejs.My code is as shown below.
I get an output of data as shown below. I am not sure why this is happening and how to prevent it.
var Converter = require("csvtojson").Converter;
var fileStream = fs.createReadStream("input.csv");
var converter = new Converter({constructResult:false});
converter.on("end_parsed", function (jsonObj) {
var jsonfile = require('jsonfile');
var file = 'output.json';
jsonfile.writeFile(file, jsonObj, function (err) { console.error(err); });});
fileStream.pipe(converter);
{"��P\u0000a\u0000c\u0000k\u0000a\u0000g\u0000e\u0000 \u0000N\u0000a\u0000m\u0000e\u0000":"\u0000c\u0000o\u0000m\u0000.\u0000t\u0000r\u0000i\u0000n\u0000e\u0000t\u0000.\u0000h\u0000r\u0000p\u0000m\u0000o\u0000b\u0000i\u0000l\u0000e\u0000","\u0000A\u0000p\u0000p\u0000 \u0000V\u0000e\u0000r\u0000s\u0000i\u0000o\u0000n\u0000 \u0000C\u0000o\u0000d\u0000e\u0000":"\u00006\u00002\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000e\u0000r\u0000 \u0000L\u0000a\u0000n\u0000g\u0000u\u0000a\u0000g\u0000e\u0000":"\u0000e\u0000n\u0000","\u0000D\u0000e\u0000v\u0000i\u0000c\u0000e\u0000":"\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000S\u0000u\u0000b\u0000m\u0000i\u0000t\u0000 \u0000D\u0000a\u0000t\u0000e\u0000 \u0000a\u0000n\u0000d\u0000 \u0000T\u0000i\u0000m\u0000e\u0000":"\u00002\u00000\u00001\u00005\u0000-\u00001\u00002\u0000-\u00002\u00002\u0000T\u00000\u00003\u0000:\u00003\u00002\u0000:\u00003\u00008\u0000Z\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000S\u0000u\u0000b\u0000m\u0000i\u0000t\u0000 \u0000M\u0000i\u0000l\u0000l\u0000i\u0000s\u0000 \u0000S\u0000i\u0000n\u0000c\u0000e\u0000 \u0000E\u0000p\u0000o\u0000c\u0000h\u0000":"\u00001\u00004\u00005\u00000\u00007\u00005\u00005\u00001\u00005\u00008\u00002\u00006\u00002\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000L\u0000a\u0000s\u0000t\u0000 \u0000U\u0000p\u0000d\u0000a\u0000t\u0000e\u0000 \u0000D\u0000a\u0000t\u0000e\u0000 \u0000a\u0000n\u0000d\u0000 \u0000T\u0000i\u0000m\u0000e\u0000":"\u00002\u00000\u00001\u00005\u0000-\u00001\u00002\u0000-\u00002\u00002\u0000T\u00000\u00003\u0000:\u00003\u00002\u0000:\u00003\u00008\u0000Z\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000L\u0000a\u0000s\u0000t\u0000 \u0000U\u0000p\u0000d\u0000a\u0000t\u0000e\u0000 \u0000M\u0000i\u0000l\u0000l\u0000i\u0000s\u0000 \u0000S\u0000i\u0000n\u0000c\u0000e\u0000 \u0000E\u0000p\u0000o\u0000c\u0000h\u0000":"\u00001\u00004\u00005\u00000\u00007\u00005\u00005\u00001\u00005\u00008\u00002\u00006\u00002\u0000","\u0000S\u0000t\u0000a\u0000r\u0000 \u0000R\u0000a\u0000t\u0000i\u0000n\u0000g\u0000":"\u00005\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000T\u0000i\u0000t\u0000l\u0000e\u0000":"\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000T\u0000e\u0000x\u0000t\u0000":"\u0000","\u0000D\u0000e\u0000v\u0000e\u0000l\u0000o\u0000p\u0000e\u0000r\u0000 \u0000R\u0000e\u0000p\u0000l\u0000y\u0000 \u0000D\u0000a\u0000t\u0000e\u0000 \u0000a\u0000n\u0000d\u0000 \u0000T\u0000i\u0000m\u0000e\u0000":"\u0000","\u0000D\u0000e\u0000v\u0000e\u0000l\u0000o\u0000p\u0000e\u0000r\u0000 \u0000R\u0000e\u0000p\u0000l\u0000y\u0000 \u0000M\u0000i\u0000l\u0000l\u0000i\u0000s\u0000 \u0000S\u0000i\u0000n\u0000c\u0000e\u0000 \u0000E\u0000p\u0000o\u0000c\u0000h\u0000":"\u0000","\u0000D\u0000e\u0000v\u0000e\u0000l\u0000o\u0000p\u0000e\u0000r\u0000 \u0000R\u0000e\u0000p\u0000l\u0000y\u0000 \u0000T\u0000e\u0000x\u0000t\u0000":"\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000L\u0000i\u0000n\u0000k\u0000":"\u0000"},
{"��P\u0000a\u0000c\u0000k\u0000a\u0000g\u0000e\u0000 \u0000N\u0000a\u0000m\u0000e\u0000":"\u0000"}
I resolved this issue: It was an encoding issue: correct code was "utf16 "
var csvEncoding = { encoding: 'utf16le' };
var csvString = fs.readFileSync(csvfile, csvEncoding).toString();
converter.fromString(csvString, function(err,result){
//your code here console.log(err);
console.log(result);
});
I had a very similar issue to OP but was using csv-parse with a file coming out of S3.
Thanks to OP I got on the right path with an encoding issue, I was able to resolve my issue by using utf16le in my stream coupled with iconv-lite like so:
s3
.getObject(getObjectParams)
.createReadStream()
.on('end', () => cb(null))
.pipe(iconv.decodeStream('utf16le'))
.pipe(parse({ delimiter: '\t', columns: true }))
.pipe(transformer);
Hopefully this helps others in the same boat!
var Converter = require("csvtojson").Converter;
var fs = require('fs');
var fileStream = fs.createReadStream("input.csv");
var converter = new Converter({constructResult:true});
converter.on("end_parsed", function (jsonObj) {
var jsonfile = require('jsonfile');
var file = 'output.json';
console.log(jsonObj);
jsonfile.writeFile(file, jsonObj, function (err,result) {
console.error(err);
console.log(result) ;
});
});
fileStream.pipe(converter);

Categories

Resources