DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on • Updated on


how to write in to csv file in python

how to write in to CSV file.

to write in to csv file is very easy with the help of
csv.writer class. The class returns a writer object that is responsible for convertion of the user’s data into a delimited string.

As I have said earlier we will need to make use of csv.writer() objects.
csv.writer(csvfile): the csvfile will be the argument in csv.writer(). Also the csv.writer class take two method for writing to CSV. They are as follow:
writerow(): to write just a row, most of the times we use it to write the heading or first row of the csv. the formula will be like:

writerows(): This method is used to write multiple rows at a time. This can be used to write rows list. the pattern of this look like:


# program for writing to CSV   
import csv  

# the heading row  
#the first row data 
header = ['S/N','PUNCHING (#)','PAY OUT','REMBLC.']
#the data rows of the csv file under the header.

# name of csv file you want to write into 
filename = "Pos.csv"
# writing to csv file  
with open(filename, 'w') as csvfile:  
    # creating a csv writer object   
    myCsvwriter = csv.writer(csvfile)  
    # write the header data 
    # writing the data rows  
   # all data written.
Enter fullscreen mode Exit fullscreen mode

you can search for the filename(Pos.csv) and open it to see the result.


Alt Text

study the code well, some of our datas a43 in quote while some are not it is a simple logic. you quote any of your datas happen to be a string while numbers are written without quotation. the method use above is a list method. each row arrange in a list and use to form another list as you can see above and that is how easy it is.
But what if our data we wish to write in to csv is in dictionary method? well, it is not a problem We can also write dictionary to the CSV file. To do this we need the CSV module provides the csv.DictWriter class. This class returns a writer object that is going to map through the dictionaries onto output rows.
the csv.DictWriter look like below
csv.DictWriter(csvfile, fieldnames)

The csv.DictWriter provides two methods for writing to CSV. They are:

  1. writeheader(): this method writes the first row of your csv file using the pre-specified fieldnames i.e header or the name you give to it.

  2. writeows(): this method writes all the rows but in each row, it writes only the value not with the dictionary key.
    let's queickly take a look at some examples.

# import the csv module  
import csv  
# my data rows as dictionary objects  
mydict =[{'name':'Abdullahi','Age':'23years','profession':'teaching','Sex':'Male'},
{'name':'Usroh','Age':'16years','profession':'software developer','Sex':'Female'}]  

# the header should be filled with the keys
header = ['name', 'Age', 'profession', 'Sex']  

# name of csv file  
filename = "Biodata.csv"

# writing to csv file  
with open(filename, 'w') as csvfile:

    # creating a csv dict writer object  
    myWriter = csv.DictWriter(csvfile, fieldnames = header)  

    # writing headers (field names)  

    # writing data rows  
    #evey thing written.
Enter fullscreen mode Exit fullscreen mode

search for the filename(Biodata.csv) and open it you should see the following result.
Alt Text

Now comment below how if you find this article helpful. consider to follow me so that you don't miss the next lesson. keep learning code.

if you have any question don't hesitate to ask. chat me up on WhatsApp or Mail. Don't forget to follow me on Twitter so that you don't miss any of my articles.

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git