data into txt file

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Computer Science

NODEJS

How do I read specific data from csv file (for example extract data only from Canada and United States) and put that into a variable and then make a txt file and use that variable to put that data into txt file? Here is what I have so far below, I'm struggling with the ////grab data for canada section. Thanks for helping.

 

const csv = require('csv-parser');

const fs = require('fs');

const inputs = [];

//use csv parser

fs.createReadStream('input_countries.csv')

.pipe(csv())

.on('data', (row) => {

inputs.push(row);

})

.on('end', () => {

console.log('CSV file successfully processed');

});

 

console.log("Deleting canada.txt file if it exists");

fs.unlink('canada.txt', function (err) {

if (err) {

return console.error(err);

}

console.log("canada.txt deleted sucessfully")

});

console.log("Deleting usa.txt file if it exists");

fs.unlink('usa.txt', function (err) {

if (err) {

return console.error(err);

}

console.log("usa.txt deleted sucessfully")

});

const header = ['country,year,population']

const Canada = [];

const USA = [];

//grab data for canada and usa

inputs.forEach((input) => {

if (input.country == 'Canada')

Canada.push(`${input.country},${input.year},${input.population}`);

if (input.country == 'United States')

USA.push(`${input.country},${input.year},${input.population}`);

});

//write data to txt file

fs.writeFile('canada.txt', Canada.join('\n'), (err) => {

if (err) {

console.log('error writing data to canada.txt', err);

}

else {

console.log('saved data to canada.txt sucessfully')

}

})

 

fs.writeFile('usa.txt', USA.join('\n'), (err) => {

if (err) {

console.log('error writing data to usa.txt', err);

}

else {

console.log('saved data to usa.txt sucessfully')

}

}.

Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education