How to write to a CSV file in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back in this mini tutorial I'm gonna show you guys how you can use these built-in CSV module to write to CSVs and python so the first thing we're going to do is import the CSV module the second thing we're gonna do is open a new file as you can see I have no files right here so we need to create a new file we can do that in python by doing this so we're going to make a new CSV comma file called names dot CSV we have to set the mode to write so that it knows that it needs to create a new file we'll do that as new CSV file now we need to set the columns that we want and you can do that in a variable called field names and here we're gonna set our field names to a list the first item let's just call it last the first name and the second item we can call last name now we're going to create our writer object so write 4 equals and then CSV the dict writer and we're gonna pass in the CSV file and the second thing we're gonna pass in is the field names and then we're gonna write our headers so we're gonna write down the we're gonna we're gonna write the the column names to the header so this is called write header and now we can start writing rows so we could do something like write writer dot write row and then we can do um in this case a dictionary with the keys first name and we can say baked and last name Dean's for example oops there we go and let's just copy and paste the same thing a couple of times just so you guys can see it works save it open up my terminal run it now you can see that this name is dot CSV file was created and you can see I have the first name column the last name column and I was able to write to those columns now this is one way of doing it I want to just show you guys quickly another way that I like to do it so what you could do is instead of hard-coding the right rows like this you can do this in like a programmatic way so I'm just gonna copy and paste this if I create a list called names just like this let me move this so you can create a list called names and then what and that it's a list of dictionaries with the first name and last name key then what you can do here in the field names is you can just do names take the first element and then call the keys function and that will return first name and last name in a list so that way you can change this list and the field names will dynamically change with that and the second thing that you can do which is neat is you could just do four row in names writer dot write row row like that and it should do the same thing so now if I run this it ran open and it worked and the last way that you can do this programmatically is you could do right rows which takes an iterable of dictionaries so you could just do right rows like that and you can get rid of this for loop altogether and just put names here and this should do the exact same thing there you go and just to emphasize that it's working let me just copy and paste it like that run it look at it there you go cool I hope you guys found this useful I'd appreciate it if you left a like subscribed and left a comment see you guys in the next one thank you
Info
Channel: Feras Alazzeh
Views: 51,351
Rating: undefined out of 5
Keywords: python, csv, write, tutorial
Id: cVxS5vfu-lQ
Channel Id: undefined
Length: 4min 54sec (294 seconds)
Published: Sun Jul 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.