Naturally if we have the ability to read different CSV files we might want to be able to programmatically create CSV files that save output and data that someone could load into their spreadsheet software. Let’s say we have a big list of data that we want to save into a CSV file. We could do the following:
big_list = [{'name': 'Fredrick Stein', 'userid': 6712359021, 'is_admin': False}, {'name': 'Wiltmore Denis', 'userid': 2525942, 'is_admin': False}, {'name': 'Greely Plonk', 'userid': 15890235, 'is_admin': False}, {'name': 'Dendris Stulo', 'userid': 572189563, 'is_admin': True}] import csv with open('output.csv', 'w') as output_csv: fields = ['name', 'userid', 'is_admin'] output_writer = csv.DictWriter(output_csv, fieldnames=fields) output_writer.writeheader() for item in big_list: output_writer.writerow(item)
In our code above we had a set of dictionaries with the same keys for each, a prime candidate for a CSV. We import the csv
library, and then open a new CSV file in write-mode by passing the 'w'
argument to the open()
function.
We then define the fields we’re going to be using into a variable called fields
. We then instantiate our CSV writer object and pass two arguments. The first is output_csv
, the file handler object. The second is our list of fields fields
which we pass to the keyword parameter fieldnames
.
Now that we’ve instantiated our CSV file writer, we can start adding lines to the file itself! First we want the headers, so we call .writeheader()
on the writer object. This writes all the fields passed to fieldnames
as the first row in our file. Then we iterate through our big_list
of data. Each item
in big_list
is a dictionary with each field in fields
as the keys. We call output_writer.writerow()
with the item
dictionaries which writes each line to the CSV file.
Instructions
We have a list in the workspace access_log
which is a list of dictionaries we want to write out to a CSV file.
Let’s start by importing the csv
module.
Open up the file logger.csv in the temporary variable logger_csv
. Don’t forget to open the file in write-mode.
Create a csv.DictWriter
instance called log_writer
. Pass logger_csv
as the first argument and then fields
as a keyword argument to the keyword fieldnames
.
Write the header to log_writer
using the .writeheader()
method.
Iterate through the access_log
list and add each element to the CSV using log_writer.writerow()
.