Snatch any course you want for just $15! Go to “Courses” and find a course ↓
Introduction
Writing CSV files
Summary

Instruction

Good job! Of course, we normally don't use writerow() for each line manually, especially when we have lots of data. Typically, we generate the data on the fly, or have the data pre-calculated and stored in a list of lists. In such cases, we can use a for loop:

data_to_save = [
  ['Author',        'Title',                   'Pages'],
  ['John Smith',    'Keep holding on',         '326'],
  ['Erica Coleman', 'The beauty is the beast', '274']
]
with open('books.csv', mode='w', newline='') as csv_file:
  csv_writer = csv.writer(csv_file)
  for row in data_to_save:
    csv_writer.writerow(row)

Inside the for loop, all you need to do is pass in a row to the writerow() function.

Exercise

Create a file named transactions.csv, and populate it with the data provided in the template.

Stuck? Here's a hint!

The code that you need to write is very similar to the one provided in the explanation. The dataset is different, but the method stays the same.