(upbeat music)
- [Instructor] Okay, let's review my code.
So here's my solution file.
So for this challenge, we had to take the inventory CSV file
and transform it into an Excel workbook
with separate worksheets for each of the categories
of the items in the file.
Well, we already know how to read a CSV file
because we saw that earlier in the course.
So the first part of my code does exactly that.
I create two variables.
One of them is a default dict that maps a string to a list
to hold each of the category names.
And the other is going to hold the first header row
so I can use it over and over again.
So this is my function to read the CSV data.
So it opens the file, reads the first row,
and then stores
that value, the first row into my global variable,
which is this headers array right here.
And again, I do this so that I can write the headers
to each worksheet later.
And then, of course, I read in each row,
and I use the value at index one,
the column index one, which remember is the category column.
So I'm going to read the category name,
and I'm going to use that value
as the key into my default dict.
And remember, because I'm defining this dictionary
to hold list values, I just append
that current row into the list that maps to this category.
So when this loop completes, I will have a dictionary
that contains all of the category names,
each of which is going to map to a list that contains all
of the associated rows for that category.
So then I just need to create the workbook
and the worksheets to go with it.
So I create the inventory dot XLSX output file,
and then I get all the keys from the dictionary,
which are now the category names.
I loop over each name, create a worksheet with that name,
and then I write the first row,
which is the header variable I saved earlier,
and that's going to happen for each worksheet.
Next, I get the list of data rows
for that category and write out each row.
I use the standard enumerate function to get both an index
and the row of data to write out.
And when the data is written,
I set the zoom value for the sheet.
I call the autofit function to make sure
that each column is expanded.
And then when the loop terminates, I close the workbook
and all the data is written out.
All right, so that's my code.
How does your solution compare to mine?