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

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: Modify CSV content

Solution: Modify CSV content

(upbeat music) - [Instructor] All right, let's examine my code for this challenge. We needed to add a new column to the inventory data to calculate the margin between the consumer prices and wholesale prices. So I already have two functions for reading and writing the CSV data. So I just needed to write the code to perform the calculations. My code starts by reading the Inventory.CSV file, and then I get the first row of the result, which is the row of headers. So I add the new string margin on the end of that list. That's the easy part, all right? So then I use an array slice to get the rows from one until the end of the file. So this is all the data, right? This is all the data after the headers. So for each one of these rows, I loop over and I calculate the difference between the values at index four and index three. And I cast each one of those to a decimal object. And you can see that if I scroll back up, I imported the decimal class from the decimal module, and I'll get to that…

Contents