Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
Introduction
Summary
12. Summary

Instruction

Good job! It's time to wrap things up.

  1. To write data into CSV files, you can use the following template code (using lists):
    with open('filename.csv', mode='w', newline='') as csv_file:
      csv_writer = csv.writer(csv_file)
      for row in data_to_save:
        ...
    
  2. If you prefer dictionaries, you can use a DictWriter instead:
    with open('books.csv', mode='w', newline='') as csv_file:
      csv_writer = csv.DictWriter(csv_file, fieldnames=['field1','field2'])
      for row in data_to_save:
        ...
    
    Use csv_writer.writeheader() to insert the header row.
  3. Instead of a for loop, you can use the writerows() method to insert multiple rows.
  4. You can specify additional parameters such as delimiter, quotechar, and quoting. For example:
    csv.writer(csv_file, fieldnames=['Author','Title','Pages'], quotechar="'", quoting=csv.QUOTE_ALL)

Excellent! How about taking on a challenge now?

Exercise

Click Next exercise to continue.