 Now that we've introduced the idea of dictionaries, we're gonna return back to structured CSV files And the entire reason why is because well CSV happens to handle dictionaries So quick super refreshing what we're talking about here is if we're dealing with a CSV file CSV stands for comma separated values And so each entry and say for example a table could also be represented as sort of little commas separating each one of the values and So now we have sort of this entry for Chicago and every single one of the values on Each row happens to also have those values as well And once again, we also introduced in our last module the idea of the CSV module. It handles All of the CSV files very similar to some of the different libraries that we've seen their job is to look at CSVs and process them And it does this by simply using something like CSV dot reader now Again, this is just going to process it as a CSV and that is it But you may once again notice that we happen to have you know this header row that Allows us to say oh well, you know the entry the first column is all cities the entry at the whatever column Would be here for Atlanta and when we're dealing with say for example a What's the word I'm looking for when we're dealing for our case of just using reader It treats everything as if it's just an entry well More specifically it would treat things as if they were in a list So if I wanted to represent a particular Column I'd use line for so to speak maybe I don't want to do that Maybe instead I want to say oh well, I do want to I think that's let's see that's zero one two three four Yeah, instead of using four and trying to you know count out each one of them Maybe I can use a dictionary and that's exactly what the dict reader will do now instead of saying CSV dot reader you just come in and say dict reader and You still pass in our our file just like we would normally do but now instead of having to if we were wanting to look for Atlanta instead of doing something like line for We would go line And we would use the header as our key at Atlanta So let's take a look at that So I happen to be using in our case this time I'm doing something known as the iris CSV super common for just example parsing But again, it just happens to have a bunch of Flowers or irises specifically and what their sepal length width pedal length Pedal width and then what specific species so there are a lot of Satosa entries and then there's colors or however You can bounce that and virginicus so the entire idea here is as you can see all of this is stored in a CSV file So in our case what I would want to do So just to load this up and I'll walk through each step as it's running So again, we load that into memory and then the first thing I'm doing here is I am creating that same dictionary reader and Once again, this will now allow me to use the header row and all of the the values in the header as My keys for each row So in that case now I go through each line and the first thing I am doing Just as this example is saying I want to extract out all of the Satosa entries for whatever reason So if the line at species so again, whatever is in the species column So first one is Satosa. So a Satosa If it equals our extracted species if it equal equals Satosa Then do the following in this case take out all of the entries For that row and I'm just storing them in I'm converting them into a floating point because you know still treat them like it's a string and Take them and give them small little short variable names that way I can then add them to a Contents file and then just print those contents. So The first 10 entries are just lists of lists as you can see the first entry 5.1 3.5 1.4 is 0.2 That's the exact entries that we see here in the first entry. So again, we're just extracting out all of those entries now Same kind of concept going on there. We also have a method for working off of our dictionary writer So the same kind of concept we had a CSV dot writer That would allow us to go in we specify the writer. We write all of our entries sort of just in using right row Well, guess what just like we had a dictionary reader. We have a dictionary writer And then the entire idea here is First you have to specify when you're building this out Not only your file that you're working off of but what are the going to be the keys and in what order again? Because we're building something to a physical file so it does need to know sort of the order and in this case I've got just some example, you know, if I was recording something like student data You know ID the exam what kind of category student. They are freshman or some analysis. I was looking at whether or not they were a completer of Work or they were a lazy student Not doing that to you. I swear Anyways the entire idea would be that oh, well you first establish those are the columns and Then the first line is oh, well just make sure that you write those headers so again if we continue with our format and to Just work off of it since I've already done Satosa as sort of an example. Let's use virginica So again, we're seeing the entries for virginica and if we were to scroll down to that first entry of virginica Which is right there six point three three, so we're seeing those values right there And so now I want to make them a new file. So first Establishing what that file is going to be called. So again, I want it in the data folder and I'm going to give it a name of extracted curly braces because the curly braces are going to specify in our case what the Extracted species is going to be so again virginica in this case Then I am saying what the headers are going to be the column headers are established as and then once again Creating an opening that file with the with command establishing our dictionary reader writing the header and then Effectively going into the row of contents this section here Unpacking all of the values so a row happens to have four values the sepal link sepal width petal link petal width extract those using some short little variables and The point I'm making here and this little part is again. We can build a dictionary And to write all those values and so if you happen to have a lot of values You're building in very massive CSV file It may be easier to just start with a small CSV or small dictionary or an empty dictionary and then Go in and add those entries in One by one and then all you would need to do is just like we would do when we are writing a row with the CSV Writer we would do writer dot right row to the entry. We take that we run it. We get no errors We take a look over in data and there's that extracted virginica that we were just talking about and There's all of our entries written to a CSV file