Skip to content

Commit a4eea8c

Browse files
committed
reading data from csv file
1 parent 28cc85c commit a4eea8c

File tree

4 files changed

+9670
-0
lines changed

4 files changed

+9670
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
package-lock.json

Planets/index.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require("fs");
2+
const parse = require("csv-parse");
3+
4+
const results = [];
5+
6+
// Note - the file path that has been provided to createReadStream("Planets/kepler_data.csv") is realtive to the package.json file to run the program as `npm run kepler`.
7+
// For running the program as `node index.js` in Planets folder, change the path accordingly to createReadStream('kepler_data.csv')
8+
9+
// to read file data in bytes
10+
fs.createReadStream("Planets/kepler_data.csv")
11+
// pipe the stream data with parse to get a json format
12+
.pipe(
13+
parse({
14+
comment: "#", // specifying that the file containing lines with # will be considered as comments
15+
columns: true, // to create json format rather than having it in the form of array
16+
})
17+
)
18+
.on("data", (data) => {
19+
results.push(data); // data will be in json format
20+
})
21+
.on("error", (err) => {
22+
console.error(err);
23+
})
24+
.on("end", () => {
25+
console.log(results);
26+
console.log("Done!");
27+
});

0 commit comments

Comments
 (0)