From the course: Advanced Hands-On Python: Working with Excel and Spreadsheet Data

Reading CSV files into an array

- [Instructor] So there are a couple of different ways to read the content of a CSV file into a Python program. In this example, we'll see how to read a CSV file into an array of arrays. And to do this, we'll need to use the CSV module in the Python Standard Library, and I've pulled that up here at this link. You can find this in the Python documentation, and I would suggest leaving this open in a browser window so you can refer back to it as we go through the example. Over in my example code, let's switch over to Visual Studio Code, I'm going to open up read_csv_array.py. You can see that I've imported the CSV module so we can make use of the code that it contains. For this example, we're going to use this file right here, inventory.csv as our sample data to read. It contains information about a variety of things you might find in a supermarket. The item name, its category, quantity in stock, and the wholesale and consumer prices for each item. I'm going to create a function to read the file and return an array of data. So let's go back to the code. All right, so this is the array that I'm going to return right here. So let's fill out this function. First, we open the file in read mode. So I'm going to write with open, and then I'm going to open the file name, and we're going to open that in read mode as, and I'll call that CSV file. So file name is the argument that's passed into the function right here. Then we need to create a new reader object using the CSV module to actually read the data. So that reader will then supply each row for us to read and add to the end of the data variable. So I'm going to create a reader from the CSV modules reader, and I'm going to pass in the CSV file that I just created. Now we need to loop over each row in the reader. So for each row of data in the reader, I'm going to append that row onto my data array. And then finally, we just return the result. So I'll return the data. Alright, so when the data has been read, we can inspect the content. So let's print out some of the data content. And remember, this data variable itself is a list that contains other lists. So each list in the array is a single row of data. So let's print out some interesting things. First, let's print out how many items there are in the list. So I'll call the length function on the inventory data. And then let's print out the first row, so I'll print out inventory data at index zero. Let's also print out the next one as well. So we'll print out row one. So row zero will be the header information. If you refer back to the file, this is row zero right here. So it's going to be the headers for the individual rows, and then row one should be this apple right here. And then let's print out some detailed data. Let's print out inventory data in row one at index zero. So that should be the word "Apple." And then let's also print out row one at index two, and that should be, let's see, index two is going to be the quantity. Alright, so let's go ahead and save this. And I'm going to run this. What I'm going to do is I'm just going to right click, and because I have that Python extension installed, I'm going to choose run Python file in terminal. And sure enough, when we do this, you can see that the result shows 51 rows. So there's 50 rows of actual data, and that includes the head of rows, there's 51 total rows. And we can see sure enough that the first row is the categories and the second row is the first row of data, and then we can access the individual data items. All right, first example, done and dusted.

Contents