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
vince
vince•2y ago
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
kingtigerknight
kingtigerknight•2y ago
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
vince
vince•2y ago
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
kingtigerknight
kingtigerknight•2y ago
ah ok, let me try that
kingtigerknight
kingtigerknight•2y ago
what I do here?
vince
vince•2y ago
You can just hit enter a bunch of times
kingtigerknight
kingtigerknight•2y ago
ah ok thanks
vince
vince•2y ago
Yea, you could also do npm init --yes that just does all that stuff for you without having to hit enter 10 times
kingtigerknight
kingtigerknight•2y ago
ah cool then I do npm i csv after right?
vince
vince•2y ago
Yep! It should install successfully
kingtigerknight
kingtigerknight•2y ago
yep
kingtigerknight
kingtigerknight•2y ago
Oh dear... what is all of this? XD
vince
vince•2y ago
And then you just read the docs for it, you'll need to import the csv package to your file.
// Import the package
import * as csv from '../lib/index.js';
// Import the package
import * as csv from '../lib/index.js';
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"
kingtigerknight
kingtigerknight•2y ago
ah ok. So for the (import * as csv from '../lib/index.js';) do I put that in a javascript file I create?
vince
vince•2y ago
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
console.log('Hello, world!');
console.log('Hello, world!');
You would run node main.js and on your command line you'll see Hello, world!
kingtigerknight
kingtigerknight•2y ago
question, for ('../lib/index.js') do I need to change that depending on where my main js is?
kingtigerknight
kingtigerknight•2y ago
heres my folder
vince
vince•2y ago
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:
cd server
npm init --yes
npm i csv
touch main.js
cd server
npm init --yes
npm i csv
touch main.js
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 works
kingtigerknight
kingtigerknight•2y ago
I copied the sample and it input this. Dose it mean it works?
vince
vince•2y ago
I think so! That's what I got too. Just to make sure, do a console.log(csv); You should see
{
generate: [Function: generate],
parse: [Function: parse],
stringify: [Function: stringify],
transform: [Function: transform]
}
{
generate: [Function: generate],
parse: [Function: parse],
stringify: [Function: stringify],
transform: [Function: transform]
}
kingtigerknight
kingtigerknight•2y ago
yep see it too. Nice!
vince
vince•2y ago
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
kingtigerknight
kingtigerknight•2y ago
ah ok, I was just about to ask if there is a guide for this package on how to read the csv file XD
vince
vince•2y ago
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
kingtigerknight
kingtigerknight•2y ago
Ah ok I see. thanks for the help 😄
vince
vince•2y ago
Np! Glad I could help 😀