Just want to clarify a few things on this one…?
From the model code explanation:
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.
^^ Q: This is done without passing any arguments in ‘.writeheader()’?
Then we iterate through our
big_list of data. Each
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.
^^Q: .writerow() automatically knows that these will be dictionary values and pulles the value data for each key in the list?
access_log comes preformatted as a list with dictionary elements, most CSV data won’t come in this format, no?
import csv with open('logger.csv','w') as logger_csv: log_writer = csv.DictWriter(logger_csv, fieldnames = fields) log_writer.writeheader() for item in access_log: log_writer.writerow(item)