Comp 110 CSVs

What is a CSV?

A CSV is special file format (which stands for "Comma-Separated Values") that we use to import data into programs. These file types allow us to manipulate and use table data like in an Excel spreadsheet!

Any data in Excel can be saved as a *.csv (CSV UTF-8) file, and will take into consideration the first row of the spreadsheet as a heading. Reference the lecture 10 lecture code for an example of this use. 

In the Joel Berry example discussed in lecture, each row of the CSV would be associated with a "Game" class - each Game has a date, opponent, points, and fouls property. Any data that you will need in lecture or for problem sets will be provided to you in the data folder in your COMP110-S19 folder.

class Game {
     date: string ="";
     opponent: string = "";
     points: number = 0;
     fouls: number = 0;

How do I import a CSV in my code?

You can use the following function!

await csvToArray(prompt:string, cname:Class): Class[]

The prompt parameter refers to a string value that will be presented to the user when they need to import their CSV (ex: "Please import a *.csv file!")

The cname parameter refers to the name of the class that you want each row of the CSV to correspond to (ex: Game)

This function will return an array of objects (of type cname) that can be accessed like any other array!

//to access the date of the 3rd game in the lecture 10 example data
let joelsGames = await csvToArray("Choose a CSV file", Game): Game[];