Created
December 15, 2018 17:18
-
-
Save shsunmoonlee/d0bc5536d8c1c52eead361db1000b264 to your computer and use it in GitHub Desktop.
csvparse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // mock test-file.csv | |
| // Cristiano Ronaldo, 2017, Real Madrid, 46, 37, 289, 458 | |
| // Leo Messi, 2015, FC Barcelona, 49, 45, 138, 513 | |
| const util = require('util'); | |
| const fs = require('fs') | |
| fs.readFileAsync = util.promisify(fs.readFile); | |
| const printNamesFromCSV = async (csvFile) => { | |
| let fields, obj | |
| let result = [] | |
| const data = await fs.readFileAsync(csvFile) | |
| const str = data.toString() | |
| const lines = str.split('\n') | |
| lines.map(line => { | |
| if(!line) {return null} | |
| fields = line.split(',') | |
| obj = { | |
| playerName: fields[0], | |
| year: fields[1], | |
| clubName: fields[2], | |
| numGoals: fields[3], | |
| numAssists: fields[4], | |
| numAerialsWon: fields[5], | |
| numShots: fields[6] | |
| } | |
| result.push(obj) | |
| }) | |
| console.log("result",result) | |
| return result.map(player => player.name) | |
| } | |
| printNamesFromCSV('./test-file.csv') | |
| /* | |
| * without async await | |
| */ | |
| // fs.readFile(csvFile, (e, data) => { | |
| // let fields, obj | |
| // const str = data.toString() | |
| // const lines = str.split('\n') | |
| // lines.map(line => { | |
| // if(!line) {return null} | |
| // fields = line.split(',') | |
| // obj = { | |
| // playerName: fields[0], | |
| // year: fields[1], | |
| // clubName: fields[2], | |
| // numGoals: fields[3], | |
| // numAssists: fields[4], | |
| // numAerialsWon: fields[5], | |
| // numShots: fields[6] | |
| // } | |
| // result.push(obj) | |
| // }) | |
| // console.log("result",result) | |
| // }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment