 Good morning evening and everything in between as you can see what we're going to be talking about today is a you know big technical term Structured text files, but what we're really looking for is what we call a CSV file or comma separated values the entire idea is You know in the past we've seen this same kind of diagram in class the entire idea is how do I represent? Data in a way that I can process it and okay Well, you know what we've done in the past is we've said oh well I could take each one of the rows inside of the table and I could Store them in a list and then I could store that list inside of another list making a nested list And so I've got multi-dimensional list going on which is crazy But what if I wanted to take that same idea? I want to take the same thing, but maybe I'm not at the point where I want to process that Data in my program just yet, and that's the entire idea is maybe I want to take that same kind of idea of storing the data separated and put it into a file and That's exactly what a CSV file is comma separated values I basically take every single one of those cells that were you know individual cells of the table and I'm just separating them on a new line Representing a row and then every value or each cell is being separated by a comma now with this in mind We talk about this as something known as the delimiter the entire idea is well, maybe what if I have Some data or some information that I want to be storing that contains a comma Say for example, we were building a giant CSV that had You know the author's name The title of the book and literally the entire contents of that book Most books have commas. I'm gonna be almost Certain on that Now the entire idea is maybe I can't use in this case a comma to separate the values because it's in there The entire idea to the delimiter is that could be anything So in this case, I'm using the pound sign or the hashtag for all the kids out there on the internet Anyways, the entire idea is I take some symbol that I'm not using in my data and say well I'm gonna use this to separate my data. And so again, this is the idea of the pound sign You could use the percent sign. There's a straight line on your keyboard You got a whole shift to get to it. It's called a pipe. You could use the pipe You could use anything you want So now I have this data how could I maybe process it or maybe I've got a file that I'm working off of in this case Say for example, I have a an iris.csv file that I'm working off of it It's inside of my data folder this time around so iris.csv Okay, fine Now if we think about that, okay Well, I know where it is I could go through and I could do the same process that I've always done I could open the file for the line and then since I you know again It's a comma separated so I do a split on a comma that would work. That's perfectly fine It doesn't do anything crazy just as a quick refresher You can also use the width version of this but what we're really looking for now is this idea well, it's a csv file and Python happens to have a csv module specifically designed for handling CSV files, so if I came in and again, I've got that iris file. This is what it looks like The entire idea to this is it's a in a sample data set Very commonly used when you're practicing and playing around and learning about how to Manipulate data and it's just got a few different column headers. So say for example, we've got Roughly speaking, I think it's a hundred and fifty different flowers of different species So for example, the first one is Satosa. I'm gonna butcher these names. So if I'm doing that I'm gonna butcher these names. I'm gonna butcher these names that one for versillicolor versillicolor and Virginica Okay, so I've got all of this data and again I've got a sepal length sepal width pedal length pedal width again the species and each one of those Data points is separated by a comma and they are floating point value. So again, like I said I have the pythons csv module that I can work off of and So again, I'm gonna just work off of good habits. So I'll come in and say with open With open there we are now again Just to put it all in there now again I have my temp file my temp file happens to be inside of core, right again So I need to jump up a directory and To do that. I use the dot dots And then I do a slash and the slash is just saying oh well, you know from a parent directory You are from a directory up now. I need to go into my data directory And again, I have the iris.csv file and I'm just gonna again call that phi Now I still haven't really done anything with the csv file just yet. I've only I've just opened the file that's where the idea of the Reader comes into play and I like to use the variable reader again That's what pretty much every resource online will also use or some variation of like your reading mostly because to turn our file from just a file into sort of being processed as a csv file csv dot reader Now in this case, I'll come in and I'll pass it in the phi So in this case, I'll just go in and for row in reader Print row and then I'll go ahead and just break again break is just to mean to stop completely And then move on with the rest of your code. So if I run this Exactly what we would expect I'm looking at the first line of my iris.csv file And in that first line is all of the values and It's done all of the parsing and separating it Into all of the individual elements inside of a list of sepal length and sepal width are Separate and then petal length petal width and species are as well So I keep on going with this but maybe just for our sake because this is my column header While it's great. We'll be using it on a little bit I don't want to actually work off of it right now So what I'm gonna go ahead and do is I'm gonna just come in and say skip First row equals false Now what this is for is this is to skip over my first row So if not actually that needs to go Above the for loop There we are if not skip First row if I have not skipped the first row Make skip first row true The entire idea here is oh well I see the first row again The only thing that I have in my first row are the column headers and they're not the data I can't do any analysis on the data with that and then I'm gonna do just one more command Continue again the continue Keyword just as a refresher basically says stop what are you doing in this iteration? Go ahead and move on to the next iteration. Just go ahead and move ahead. Please continue From there maybe I want to do some just quick little Analysis I want to say for example just see what all the species are Well again if we look at this row when we printed it printed everything in a list And if it's a list that means we can access it via all of the Indices that are in that list so in our case Species is at zero one two three four species at row four. So print row at four And what we should see is a long string of in our case of virginica because that was The last entries and then we get to the one I really can't pronounce and then Satosas awesome But maybe I want to go one step further right maybe I want to do I want to find out what the average Satosa flower looks like well to do that I'm going to create what I like to call a running total so in our case. I'm going to do a little cheating Not cheating if it's work we've already built and in this case. I'm just going to Do some quick Copy or deleting there. We are I don't need species And I'll just come in and do a total sepal length total Seeper width total petal length and total petal width Each one of those are going to start out at zero At the beginning because again we're dealing with a running total So I haven't added anything quite yet The next little bit is I'm going to do that same paste and that same kind of approach here But now again since I know all of the Locations of all of the data points right see building will be at index zero width will be at index one two three four What I'm going to go ahead and do is I'm going to save each one of those at it as a variable so again one two three and four From here again, like I said, I want to deal with maybe for our sake of the average. What is the average? Satosa look like so. Okay. All right Now that I've saved the species I can use that as a variable. So if species equals Tosa Oh, all right. Well, then I'll take my Total value of variables that I've been working off of and let me actually just Increase this so I have a lot of working room and paste it Now I'm not just going to set them to zero. I am going to do some plus equaling Specifically on the other variables that I've saved so the sepal length width length width in this case float length and I'll just go ahead and Copy out those Now your hand-eye coordination as you notice that's it just you got to practice that Well, it was a lot of copy and pasting and then clicking and double-clicking a lot of things Just as if you didn't know that you can double-click on something and get it copy Paste it. So it's a really great tool but the other thing I need to know is how many of Satosa's war there as of right now, I don't know and so I'm going to go ahead and also make a count variable set it equal to zero and Every time I see a Satosa. I'll go ahead and just do a plus equals one Fantastic because now Once I'm done with all of this processing right what I'm going to do is I'm going to exit out of the processing phase And what I mean by that is I'm not going to be tabbed inward I'm going to go ahead and be tabbed out remember While we have a file open If things happen if it gets corrupt or something of that nature, you know, we run into some slight problems So we really want to be when we done Processing a file immediately close it back up So in our case now that I've done all of this process the last thing I'm going to do is I'm going to go ahead and come in here And I'll go ahead and cheat with this Total all of these will be switched over to AVG short for average And then let's see that'll be total sepal total width and boom divided by So now that I've gone through all of that I can do a print out of each one of those and in our case I'll just do a quick one nothing terribly crazy, but there There I think they Added the spacing because I added the inners inside of the space And so what I should see when I print this is I should see the sepal length is roughly about Five I guess inches or centimeters. I'd have to double check on the data set Sepal width is about 3.4. Give or take almost Pedal length is about a 1.5 and then the pedal width is a Almost about a quarter. So there you are