Reading, flitering, and showing CSV files
Hello, I was wondering if anyone know a website somewhere or know how to read csv files with javascript and also filter it? I have google through many pages however it seems to be popular with python but I was wondering if anyone knew a way with javascript?
26 Replies
Yes, check out NPM's csv package
Just did it yesterday haha
https://www.npmjs.com/package/csv
This is for Node.js environment, so if you're doing it purely with the browser, I'm not sure, someone here should know though
Ah nice! I'll check it out, I'm new to Node.js so I'm not really sure how this is going to work out XD
Np, if you have some questions feel free to ask. I ended up using the
csv-parser
package (which is separate from the one I linked you) so I'm not really sure how to use csv-parser
, but if you need help on how to set up your Node.js environment I should be able to help you
You'll need to install npm
and do a npm init
in your directory before you can do npm i csv
ah ok, let me try that
what I do here?
You can just hit
enter
a bunch of timesah ok thanks
Yea, you could also do
npm init --yes
that just does all that stuff for you without having to hit enter 10 timesah cool
then I do npm i csv after right?
Yep!
It should install successfully
yep
Oh dear... what is all of this? XD
And then you just read the docs for it, you'll need to import the csv package to your file.
To be honest, I'm not a node expert so I'm sure there's a bajillion better places to get an explanation, but basically the file on the right is saying "this is the project name, this is the version, and these are all the packages that the project needs"
ah ok. So for the (import * as csv from '../lib/index.js';) do I put that in a javascript file I create?
I believe so, if you create a file like
main.js
you add that to the top of the file
And when you want to run that file, you use the node command (assuming you already have node installed)
So if you have something like
You would run node main.js
and on your command line you'll see Hello, world!
question, for ('../lib/index.js') do I need to change that depending on where my main js is?
heres my folder
If I'm understanding correctly, you should install your node packages in the directory of your node files
So if your node files are under a folder called
server
you should:
Ah I see what you're saying, yea that import statement is just an example
1 second
Okay, instead of import, you use const csv = require('csv');
Let me know if that worksI copied the sample and it input this. Dose it mean it works?
I think so! That's what I got too. Just to make sure, do a
console.log(csv)
; You should see
yep see it too. Nice!
Awesome 🥳 I'm not quite sure how the package works (I've used the separate package csv-parser for the first time yesterday) so your best bet is google and the docs
But at least you got it set up
ah ok, I was just about to ask if there is a guide for this package on how to read the csv file XD
I think the package is just a collection of 4 separate csv packages, found in the dependencies tab of the npm page, so if you need specific help for generate/parse/stringify/transform, there should be docs for each of them on their separate npm pages
Ah ok I see.
thanks for the help 😄
Np! Glad I could help 😀