oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Writing a CSV File #362

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

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.

oldoc63 commented 1 year ago

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 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.