In this video, we're going to look at how to Write Data into a CSV File using Python
Video Transcription
Hi, my name is Art and I teach Python at Noble Desktop. In this video, I'm going to show you how to write data into a CSV file. A CSV file is a comma-separated values file, which is a format used to get data into and out of Excel.
If you haven't seen my previous video on how to write data into a text file, please watch it, as it will make more sense.
First, Python comes with built-in modules, such as the CSV module. I'm going to use import CSV module. Then, I'm going to create a variable name file. I use file because it makes more sense. I use the function open, which I showed in my previous video.
I have to come up with a file name, so I'm going to call it myCSVfile.csv. Then, I'm going to use the module a (a stands for append), and then the writer.
Next, I need to come up with some data. I'm going to use a list of lists, where each list will contain data (e.g. 10, 20, 30, 40, 50, 60, 70, 80, 90). To write this data into the CSV file, I'm going to iterate through the list, create a row variable, and then write the data into the CSV file. Finally, I need to save the data and close the file.
If you run this data, you should see the same CSV file. You can open it with Numbers (Mac) or Microsoft Excel. You should see the same data (10, 20, 30, 40, 50, 60, 70, 80, 90). Thank you, and I'll see you in the next video.