Last active
June 6, 2023 08:33
-
-
Save psenger/8f795470bb57b8d6d54e1f9f94f65c8e to your computer and use it in GitHub Desktop.
[Best way to load CSV's without csv-parser] #JavaScript #CSV
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
const {readFile} = require("fs").promises; | |
const run = async () => { | |
const loadCSV = async (fileName ) => { | |
const contents = await readFile(fileName, 'utf8'); | |
const lines = contents | |
.split('\n') | |
.filter(Boolean) | |
.map((t)=>t.trim()) // remove leading and trailing \r for non-utf8 files | |
.map((line)=>{ | |
// this regex, allows "quoted strings", and non-quoted strings in a csv. | |
const cells = line.match(/(".*?"|[^",]+)(?=\s*,|\s*$)/g) | |
// Map removes the quotes from the quoted strings. | |
// I advise not to trim each cell because someone might have put white space into a cell that is quoted. | |
const [ col1, col2, col3, col4 ] = cells.map(cell=>cell.replace(/^"(.+(?="$))"$/, '$1')) | |
return { col1, col2, col3, col4 } | |
}); | |
console.log(`Read ${lines.length} from ${fileName}` ) | |
return lines | |
} | |
const records = [ | |
...await loadCSV( 'some-data.csv' ) | |
] | |
} | |
run().then(()=>{console.log('done')}).catch(console.error).then(() => process.exit(0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment