Hi, welcome to this new video. In this video we're going to be learning how to read data from a CSV. We just learned what a CSV file is. It's a comma separated value file, and we have constructed data with our writer script that is a CSV file. So we actually have a CSV file. If you don't have one, go to the previous video where we construct one. You can also use something from the Internet, but it might not be formatted the same way in which we have it here. Let's cover a few ways, because you're going to realize that there's many ways in which you can read data. Often we're relying in Python, we're relying with functionality that is either inbuilt into Python or external libraries that might become a little bit more complicated, but certainly going to offer us more functionality, right? So with processing, right, we have an inbuilt reader function which we already covered in order to read a TXT file. As you can see here, we have a reader file that it's reading a CSV file we can go through with the read line method. We can skip the header, if you don't want to read the header, you would just want to read the data. You can basically use that to read each line. And we already saw how to use a while statement to loop through the data. The only difference here, as you can see in this example, is that we're using the split line dot split and saying separate this line wherever you see a comma, because we know that comma is going to be the key value that separates one piece of data from another piece of data. So the split line function is going to allow us to access multiple entries and we have to use the different index numbers. So data 0 is going to be the first entry, data 1 is going to the second, and so forth,right? So we have this file for you in the exercises examples, but we're going to learn a different version of how to do this using an inbuilt CSV library in Python. That introduces us to a new concept, which is the width statement, right? And this is something that you might come across so it's good that we discuss a little bit what this is, right? So a width statement is a specific statement that would allow us to open a file in this case and would give us a space in which we could a context, if you want, in which those resources are going to be available for us, right? So if we're trying to read the example text file, as you can see on the right, the width statement it's going to create a context in which that text file is in fact available. The great thing about the width statement in Python is that it will actually take care of the cleanup automatically, would actually do the cleanup or the sequence that might be required to kind of close that resource once we finish reading it. So if we move forward with a code. It's a very clean way of operating within a space in which we need those resources, access those resources and then we move on and the programming language will take care of the cleanup state. So let's see how that would actually look. It would look slightly different if we use this kind of CSV. We have to import CSV in our code and this would give us access to a different kind of syntax csv.reader. We can actually access the same CSV file here and we use next to move to the next line similarly to what we were doing before. But in this case the way we kind of skip to the next line is with the next keyword, right? And then the for loop becomes a little bit easier because we do a four row, right? So each row could be represented as an entity within the data that was obtained from the reader, right? So similarly we have to access entries like the index 0 entry will represent the x, index 1, it will represent the y, and so forth. So we're going to see how to access the data using this index information. So let's jump into processing and put this together and think through it as we write the code. So here we are in processing, right? And the first thing that we want to do is import our CSV module, right? That's going to give us access to the CSV reading functionality built into Python, right? And again, there's even more powerful versions of this using other external libraries. But again, it's good to know that there's multiple ways of doing the same thing. So let's start by creating our with statement, right? So with open, right? So we're going to open the data.csv. So that's going to open that and we're going to say us, this is basically saying when we open this file, let's call it CSV file, right? So we're actually creating a variable within this statement that is the CSV file variable, right? And here data, that is the data that is contained within that file, it's going to be using the csv.reader, right? So notice that we're using, similar to how we have been using random in the past, we are using CSV now, that's not a variable that's the module that we imported that contains the reader function and we can read this CSV file. The CSV file, remember, is going to be what we opened here, right? And because we're doing it within the width statement, this resource of the data containing the CSV file will only be available in the block defined by the width statement. Anything that is indented here, right? So at this point, you see we are operating within the loop or within this block, the width statement. This is not necessarily a loop, right? This is just an area in which, similar to an if statement, but it's a condition in which the resources are available. What kind of functionality can we have we can use next, right? That's in the case that we basically moving the head the same way in which we are starting to read the first line, the second line, and so on, right? At this point, if you want, we could print, let's print the data just to see what we have so far, right? So right now what we have is not data itself, we cannot see it. It's actually giving reference to this specific kind of object, right, this data. So, how do we access the actual data? So let's do a loop. So for a row in data. So, we need to understand that this data structure, which is in fact an object, contains a series of rows, right? So let's just print now, Now, right, So let's see what we're getting. So here we are starting to see the data that it's inside the object, right? And because this CSV file structure or this kind of module is doing a little bit of content, it's content aware in a way. It's going to to structure that data already in a way that it's easier to access, right? And here we're going to start addressing one of the topics that we haven't really covered in this course. But it's going to be the central focus of course 2, which is collections and data structures and in this case a list, right? But for now we want to make sure that we access, similar to what we have done with tuples. How do we access an entry in a collection, right? So let's imagine, or let's just remember what we wrote last lesson. So X equals, it's a float, we know it's a float from the row. And because we printed all the rows, right? We want to access the first entity within that row, right? So that should represent the x variable. Let's print it and see if in fact that's working right? So you can see now that we're only printing the x, which is the first coordinate that we store in the file. We're going through all the rows and we are obtaining the value of the x variable by accessing the first entry within the row list. For now, we need to understand that a list is very similar to a tuple. It has some differences that we're going to be covering in course 2. But for now we can basically use the same syntax to access entities within that. So if we have row, the x variable, we can probably access all of them, right? Reconstruct what we have done for y. And in this case would be the second entry. And here maybe you want to give a little bit of space for r, g and b, right? So this is r, this is g, And this is b, right? And here the numbers would be the second, sorry, the third or fifth element. So knowing what we know the data, just by looking at the data and understanding how many columns do we have. We have x and y representing first and second entries, and the next three entries represent the color data, right? So we no longer need the print. If we wanted to visualize this, we can do what we did before. So r, g, b and ellipse, x, y, 20, 20. And this is exactly what we done in the past. Let's see if this actually works, there we go. So we reconstructed the drawing. This drawing was originally made, created using random functions, right? So every time that you press play it would be slightly different and you can actually save it to a data file as we did last week. And now we can read it and reconstruct it with the exact coordinates that the circles had and the exact colors that they had. Even if they were random initially, we can reconstruct the data by saving it into a CSV and loading it once again. So this is kind of a different technique of doing it. We certainly wanted to introduce the width statement. Which is something that you might see as you kind of Google and kind of try to find examples of data visualization or any kind of example that uses resources, external resources such as data files, CSV files or JSON files or other kind of files. You probably will see this structure, the width statement. So this is how we could use it. Again, the notion of lists, it's a very important concept that we're going to be covering in future lessons. So I look forward to sharing that with you in future videos, so I'll see you then.