1 00:00:00,002 --> 00:00:05,004 (upbeat music) 2 00:00:05,004 --> 00:00:08,001 - [Instructor] Okay, let's review my code. 3 00:00:08,001 --> 00:00:09,009 So here's my solution file. 4 00:00:09,009 --> 00:00:13,006 So for this challenge, we had to take the inventory CSV file 5 00:00:13,006 --> 00:00:16,003 and transform it into an Excel workbook 6 00:00:16,003 --> 00:00:19,001 with separate worksheets for each of the categories 7 00:00:19,001 --> 00:00:21,005 of the items in the file. 8 00:00:21,005 --> 00:00:24,006 Well, we already know how to read a CSV file 9 00:00:24,006 --> 00:00:27,005 because we saw that earlier in the course. 10 00:00:27,005 --> 00:00:32,002 So the first part of my code does exactly that. 11 00:00:32,002 --> 00:00:34,002 I create two variables. 12 00:00:34,002 --> 00:00:39,003 One of them is a default dict that maps a string to a list 13 00:00:39,003 --> 00:00:42,000 to hold each of the category names. 14 00:00:42,000 --> 00:00:45,000 And the other is going to hold the first header row 15 00:00:45,000 --> 00:00:48,005 so I can use it over and over again. 16 00:00:48,005 --> 00:00:52,007 So this is my function to read the CSV data. 17 00:00:52,007 --> 00:00:56,000 So it opens the file, reads the first row, 18 00:00:56,000 --> 00:00:57,002 and then stores 19 00:00:57,002 --> 00:01:01,000 that value, the first row into my global variable, 20 00:01:01,000 --> 00:01:03,009 which is this headers array right here. 21 00:01:03,009 --> 00:01:06,009 And again, I do this so that I can write the headers 22 00:01:06,009 --> 00:01:10,002 to each worksheet later. 23 00:01:10,002 --> 00:01:13,003 And then, of course, I read in each row, 24 00:01:13,003 --> 00:01:16,003 and I use the value at index one, 25 00:01:16,003 --> 00:01:21,001 the column index one, which remember is the category column. 26 00:01:21,001 --> 00:01:23,002 So I'm going to read the category name, 27 00:01:23,002 --> 00:01:25,005 and I'm going to use that value 28 00:01:25,005 --> 00:01:28,007 as the key into my default dict. 29 00:01:28,007 --> 00:01:31,003 And remember, because I'm defining this dictionary 30 00:01:31,003 --> 00:01:34,002 to hold list values, I just append 31 00:01:34,002 --> 00:01:39,003 that current row into the list that maps to this category. 32 00:01:39,003 --> 00:01:42,008 So when this loop completes, I will have a dictionary 33 00:01:42,008 --> 00:01:45,009 that contains all of the category names, 34 00:01:45,009 --> 00:01:49,003 each of which is going to map to a list that contains all 35 00:01:49,003 --> 00:01:52,008 of the associated rows for that category. 36 00:01:52,008 --> 00:01:55,009 So then I just need to create the workbook 37 00:01:55,009 --> 00:01:58,000 and the worksheets to go with it. 38 00:01:58,000 --> 00:02:04,001 So I create the inventory dot XLSX output file, 39 00:02:04,001 --> 00:02:07,001 and then I get all the keys from the dictionary, 40 00:02:07,001 --> 00:02:09,004 which are now the category names. 41 00:02:09,004 --> 00:02:14,003 I loop over each name, create a worksheet with that name, 42 00:02:14,003 --> 00:02:16,002 and then I write the first row, 43 00:02:16,002 --> 00:02:18,007 which is the header variable I saved earlier, 44 00:02:18,007 --> 00:02:21,009 and that's going to happen for each worksheet. 45 00:02:21,009 --> 00:02:24,006 Next, I get the list of data rows 46 00:02:24,006 --> 00:02:29,004 for that category and write out each row. 47 00:02:29,004 --> 00:02:33,006 I use the standard enumerate function to get both an index 48 00:02:33,006 --> 00:02:37,004 and the row of data to write out. 49 00:02:37,004 --> 00:02:39,006 And when the data is written, 50 00:02:39,006 --> 00:02:42,006 I set the zoom value for the sheet. 51 00:02:42,006 --> 00:02:45,003 I call the autofit function to make sure 52 00:02:45,003 --> 00:02:47,003 that each column is expanded. 53 00:02:47,003 --> 00:02:51,003 And then when the loop terminates, I close the workbook 54 00:02:51,003 --> 00:02:54,005 and all the data is written out. 55 00:02:54,005 --> 00:02:56,003 All right, so that's my code. 56 00:02:56,003 --> 00:02:58,000 How does your solution compare to mine?