Comp 101 CSV Processing

What is a CSV?

A CSV, or Comma-Seperated Values, is a file format that we use to import data into our programs. This allows us to manipulate table data like excel spreadsheets!

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. In Excel, we have cells in which we put our information, but in a CSV our values are separated by commas instead, which allow us to work with it in our code!

Below is an example of what the data in a CSV of music albums might look like, where the commas represents a cell in Excel. 

Using the data above as an example, each row of the CSV could be associated with a "Album" object from an "Album" class. Each Album has a release date, name, genre, artist and an active from property. 

class Album {
     releaseDate: number = 0;
     albumName: string = "";
     genre: string = "";
     artistName: string = "";
     activeFrom: number = 0;
}
How would you write the code for an album class using a constructor

How do I import a CSV into my code?

All the data you need for this class can be found in the data folder in your COMP101-S19 folder. To import a CSV into your code, you 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: Album)

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

If we wanted to import our Album example, we could use the following code:

let chart = await csvToArray("Select Album CSV", Album);

We could then print the 3rd album name in our list like this:

print(chart[2].albumName);