I have .log file and I need to get this file in javascript and convert to JSON. I try this
var j= readTextFile("log991/sensorData.log");
console.log(j);
var jsonnn = JSON.stringify(j);
console.log(jsonnn);
But I only get path in console log. Is there any way to make this?
this is how .log file looks
2018-04-03 15:47:58,873 INFO log(17) batteryCurrent=-0.45, solarCurrent=3.27,
hybridCurrent=0, batteryVoltage=12.88, solarVoltage=13.09
2018-04-03 15:48:00,074 INFO log(17) batteryCurrent=-0.45, solarCurrent=3.27,
hybridCurrent=0, batteryVoltage=12.88, solarVoltage=13.09
2018-04-03 15:48:01,274 INFO log(17) batteryCurrent=-0.4, solarCurrent=3.28,
hybridCurrent=0, batteryVoltage=12.89, solarVoltage=13.1
thnx
try this code used synchronous version
const fs = require('fs');
var text = fs.readFileSync('/somepath/a.txt','utf8')
console.log (text)
try this code to convert into json
const fs = require('fs');
var text = fs.readFileSync('/somepath/a.txt','utf8')
array = text.split("\n")
var dataArray = [];
for(var i=0; i<array.length; i++){
if(array[i] == ''){continue}
let tempArray = []
tempArray = array[i].split(",");
dataArray.push(tempArray)
};
json = {};
var c = 1;
dataArray.forEach( (e1) =>{
isdate = true;
var tempjson = {};
e1.forEach( (e2) =>{
var key;
if(isdate ) {
key = 'date';
tempjson[key] = e2;
isdate = false;
}
else if(e2.includes("batteryCurrent")){
key = "batteryCurrent";
tempjson[key]= e2.split("batteryCurrent=")[1]
}
else{
var arr = e2.split("=");
key = arr[0].trim();
tempjson[key] = arr[1];
}
})
json[c] = tempjson;
c++
});
console.log(json)
Are you using node?
const fs = require('fs')
fs.readFile('log991/sensorData.log', 'utf8', (err, data) => {
console.log(data)
}
Use the readFile method of fs module.
var fs = require('fs')
fs.readFile('log991/sensorData.log', 'utf8', function(err, data) {
console.log(data)
});
Ok, i fix this with python
import json
a = open('log991/sensorData.log','r')
text = a.read()
text_as_list = text.split('\n')
keys = text_as_list[2].split()
result = []
for item in text.split('\n')[4:len(text_as_list)]:
temp_dict = {}
for i,j in zip(keys,item.split()):
if j.isdigit():
temp_dict[i] = int(j)
else:
temp_dict[i] = j
result.append(temp_dict)
print (json.dumps(result))
Related
I try to write a code that will create a file with an amount of random jokes using this npm and Environment Variables in an env file but I can't get it to work.
here is the code:
let oneLinerJoke = require("one-liner-joke");
require("dotenv").config();
let fs = require("fs");
var getRandomJokeWithTag = oneLinerJoke.getRandomJokeWithTag(
`${process.env.JOKE_SUBJECT}`
);
let arrayJokes = [];
let i = 0;
let counter = 0;
let amount = parseInt(`${process.env.JOKE_AMOUNT}`);
while (i <= amount) {
let joke = oneLinerJoke.getRandomJokeWithTag(process.env.JOKE_SUBJECT);
joke = joke.body.replace(",", "");
if (arrayJokes.includes(joke)) {
arrayJokes.push((joke + "\n"));
i++;
}
}
const createFile = fs.writeFileSync('./created_files/jokes.txt', arrayJokes.toString().replace(/,/g, ""));
Add __dirname to specify the directory.
const createFile = fs.writeFileSync(__dirname + '/created_files/jokes.txt', arrayJokes.toString().replace(/,/g, ""));
I have html with tables and in html I included js script bom.js:
$(document).ready(function() {
setTimeout(function() {
var nums = [];
$('#table_Serv tr td.serv-nomer').each(function (elem, ind) {
nums[parseInt($(this).text())] = elem + 1;
// nums.newNum[elem] = elem + 1;
// nums.oldNum[elem] = parseInt($(this).text());
$(this).text(elem + 1);
});
$('#test1234 tr td:nth-child(2)').each(function() {
// extract each number in an array
const numbers = $(this).html().split(',').map(x => Number(x));
// var sortednum = [];
var arrs = []
for(var i = 0; i <= numbers.length; i++) {
arrs[numbers[i]] = nums[numbers[i]];
}
// console.log(sortednum);
// // Sort the numbers
const sorted = arrs.sort((a, b) => a > b);
;
const stringArray = sorted.reduce((tmp, x) => `${tmp},${x}`);
// Insert the string back in the td
$(this).text(stringArray);
});
}, 1000);
});
When I go to html page, script working and get me table with changes by javascript. When jsdom get this html, jsdom is not executing javascript and get me clean html.
My index.js:
'use strict';
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
var fs = require('fs');
const options = {
resources: 'usable',
runScripts: 'dangerously',
};
var url = process.argv[2] !== undefined ? process.argv[2] : 'http://google.ru';
JSDOM.fromURL(url, options).then(dom => {
setTimeout(function() {
var cont = dom.window.document.getElementsByTagName('html')[0].innerHTML;
console.log(cont);
var file = url.split('/');
var filename = file[file.length - 1];
fs.writeFile("file.html" + filename, cont, function(err) {
if(err) {
return console.log(err);
}
// console.log("The file was saved!");
});
}, 1000)
});
How I can resolve this solutions? I need save html after javascript processing. Get html by javascript. How I can do it?
I tryied this:
JSDOM.fromURL(url, options).then(dom => {
dom.window.onload = function() {
var cont = dom.window.document.getElementsByTagName('html').[0].innerHTML;
console.log(cont);
var file = url.split('/');
var filename = file[file.length - 1];
fs.writeFile("file.html" + filename, cont, function(err) {
if(err) {
return console.log(err);
}
// console.log("The file was saved!");
});
};
});
Not working :( window.onload method is not want working..
I need to store XML data to a hashmap, I'm using nodejs and xmldom npm module to parse the XML.
I'm trying to store the testsuite name, testcase name and dt_value in a hashmap.
here is my XML code
<testscenario>
<testsuite name="com.edge.route">
<testcase name="tc_Login">dt_Login</testcase>
<testcase name="tc_Logout">dt_Logout</testcase>
</testsuite>
<testsuite name="com.edge.beacon">
<testcase name="tc_Channel">dt_Channel,dt_Logout</testcase>
</testsuite>
</testscenario>
Here's what I have tried so far
var DOMParser = require('xmldom').DOMParser;
var parser = new DOMParser();
var HashMap = require('hashmap');
var fs = require('fs');
module.exports = {
testScenario: function() {
var suiteName;
var data;
var map = new HashMap();
//read the testscenario.xml
data = fs.readFileSync("./testscenario.xml", "utf8");
var dom = parser.parseFromString(data);
var testSuiteList = dom.getElementsByTagName("testsuite");
//loop through all the test suites
for (i = 0; i < testSuiteList.length; i++) {
//select the test suite with the given name
suiteName = testSuiteList[i].getAttribute("name");
var tcList = testSuiteList[i].getElementsByTagName("testcase");
var dtList = testSuiteList[i].getElementsByTagName("testcase")[0].childNodes[0].nodeValue;
console.log(dtList)
//get the row count
tcLength = tcList.length;
//push column headers as the key in the hashmamp
var testCaseList = [];
for (x = 0; x < tcList.length; x++) {
testCaseList.push(tcList[x].getAttribute("name"));
}
console.log(testCaseList)
var dataTableList = [];
for (i = 0; i < tcLength; i++) {
dataTableList += tcList[i].childNodes[0].nodeValue;
}
console.log("dtlist = " + dataTableList);
//push the row values as an array to the hashmap
map.set(suiteName, testCaseList);
}
return [map]
}
};
I'm able to get the key, value pair for testsuite and testcase but I also need to get the dt_name. how can I modify this code to store the dt_name along with testsuite and testcase names in that hashmap?
Alright figured it out. This is how I did it. I have used a hashmap within a hashmap
//XML Reader
var DOMParser = require('xmldom').DOMParser;
var parser = new DOMParser();
var HashMap = require('hashmap');
var fs = require('fs');
module.exports={
testScenario: function ()
{
var suiteName;
var data;
var map = new HashMap();
//read the testscenario.xml
data=fs.readFileSync("./testscenario.xml","utf8");
var dom = parser.parseFromString(data);
var testSuiteList = dom.getElementsByTagName("testsuite");
//loop through all the test suites
for (i=0;i< testSuiteList.length; i++) {
//select the test suite with the given name
suiteName = testSuiteList[i].getAttribute("name");
var tcList = testSuiteList[i].getElementsByTagName("testcase");
//get the row count
Length=tcList.length;
//push column headers as the key in the hashmamp
var testCaseList = new HashMap();
for(x=0;x<Length;x++)
{
testCaseList.set(tcList[x].getAttribute("name"),tcList[x].childNodes[0].nodeValue);
}
//push the row values as an array to the hashmap
map.set(suiteName,testCaseList);
}
return [map]
}
};
I'm having a problem where for(var x=1; x < 6; x++) is getting called because too fast axios.get() is async, but I have no idea how to counter that without the solution being too complicated
const axios = require("axios");
const cheerio = require("cheerio");
function imdbGetData(id) {
var title, show, $;
var arr = [];
var airdates = [];
show = {
seasons: []
};
axios.get(`http://www.imdb.com/title/${id}/`).then((body) => {
$ = cheerio.load(body.data);
title = $("div h1").text()
});
for(var x=1; x < 6; x++) {
console.log(x); // Will count too 1,2,3,4,5,6
url = `http://www.imdb.com/title/${id}/episodes?season=${x}`
axios.get(url).then((body) => {
$ = cheerio.load(body.data);
console.log(x);// 6, 6, 6, 6
$("div .info .airdate").each(function(index, item) {
var airdate = String($(this).text());
airdates.push(airdate.trim());
});
$(".info strong a").each(function(i, item){
var airdate = airdates[i];
var epsiode_name = $(this).text()
if (epsiode_name && !epsiode_name.includes("#"))
arr.push({epsiode_name, airdate});
});
show.seasons.push(arr);
arr = []
// console.log(show.seasons);
});
setTimeout(() => {console.log(show.seasons)}, 10000) // ghetto
}
}
// season = {
// seasons: [[ {epsiode_name} ], [{Epsiode name}]]
// }
imdbGetData("tt2193021");
You can construct and push all promises to array, and then use Promise.all(arrayOfPromises). This way you will keep your asynchronous chain and you can easily handle results very similar to regular single asynchronous operation:
var promises = [];
for (var x = 1; x < 6; x++) {
url = `http://www.imdb.com/title/${id}/episodes?season=${x}`
promises.push(axios.get(url));
}
Promise.all(promises)
.then(body => {
// all results of promises will be in 'body' parameter
})
.catch(err => console.error(err));
You can also use async/await (in newer versions of Node.js), so you can make the code a little easier to read, I've made a few little changes to update progress too.
const axios = require("axios");
const cheerio = require("cheerio");
async function imdbGetData(id) {
var title, show, $;
var arr = [];
var airdates = [];
show = {
seasons: []
};
console.log('Getting from ' + `http://www.imdb.com/title/${id}/`);
let body = await axios.get(`http://www.imdb.com/title/${id}/`);
$ = cheerio.load(body.data);
title = $("div h1").text()
for(var x=1; x < 6; x++) {
console.log('Getting season: ' + x); // Will count too 1,2,3,4,5,6
url = `http://www.imdb.com/title/${id}/episodes?season=${x}`
let body = await axios.get(url);
$ = cheerio.load(body.data);
$("div .info .airdate").each(function(index, item) {
var airdate = String($(this).text());
airdates.push(airdate.trim());
});
$(".info strong a").each(function(i, item){
var airdate = airdates[i];
var epsiode_name = $(this).text()
if (epsiode_name && !epsiode_name.includes("#"))
arr.push({epsiode_name, airdate});
});
show.seasons.push(arr);
arr = []
}
console.log("Result: ", show.seasons);
}
imdbGetData("tt2193021");
You can simply use ES6 let instead of var , your code will be:
for(let i=0; i<length; i++){
asyncCall(function(){
console.log(i);// will print 0,1,2,3,...
});
}
Please check this article https://codeburst.io/asynchronous-code-inside-an-array-loop-c5d704006c99
I'm trying to log an array of numerically named sub-directories in "e:\subdirectory\" by using fs.statSync but I keep getting the error "module.exports" is not a function; to my understanding, this is exactly how I'm supposed to export the data
I'm using the synchronous version because I want the array to finish populating before it's exported.
this is for a "proof of concept", I plan on serving an html doc and pushing this array to an input field
here is the code...
checke.js
var fs = require('fs');
function checkE() {
for (var i = 1, accts = [], path = "e:\\subdirectory\\"; i <10000; i++ ) {
var target = fs.statSynch(path + i.toString())
if (target.isDirectory()) { accts.push(i) }
}
}
module.exports(checkE)
init.js
var checke = require('./checke.js')
console.log(checke)
You need to assign something to module.exports not call it like a function. module.exports is an object
checke.js
var fs = require('fs');
module.exports = {
checkE: function checkE() {
var accts = [];
var path = 'e:\\subdirectory\\';
for (var i = 1; i <10000; i++ ) {
var target = fs.statSync(path + i.toString())
if (target.isDirectory())
accts.push(i);
}
return accts;
}
}
init.js
var checke = require('./checke.js');
checke.checkE();