how to write in to csv file in python
Tags: tutorial, python, devops, newbie
Published: 2021-01-24 01:19:53



### 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:
writerow(firstrow)

`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:
writerows(rows)

Example:
```python
# 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.
rows=[[1,50100,49500,150500],
[2,100000,98600,51900],
[3,2100,2000,49900],
[4,10000,9800,40100],
[5,10200,10000,30100]]

    
# 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 
    myCsvwriter.writerow(header)  
    # writing the data rows  
    myCsvwriter.writerows(rows) 
   # all data written.
```
you can search for the filename(`Pos.csv`) and open it to see the result.

Result

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bwjuxbiqi9tcka37pagw.JPG)

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.
Example:
```python
# import the csv module  
import csv  
# my data rows as dictionary objects  
mydict =[{'name':'Abdullahi','Age':'23years','profession':'teaching','Sex':'Male'},
{'name':'praise','Age':'44years','profession':'engineer','Sex':'Female'},
{'name':'John','Age':'34years','profession':'developer','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)  
    myWriter.writeheader()  
        
    # writing data rows  
    myWriter.writerows(mydict)  
    #evey thing written.
```
search for the filename(Biodata.csv) and open it you should see the following result.
Result
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/whio3itv3hpat0m7kgxe.JPG)

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](https://wa.me/2349153036869?text=Hey!%%20Maxwizard%20l=I%20need%20your%20assistance%20from%20dev) or [Mail](mailto:aoladejo382@stu.ui.edu.ng?Subject=Hey!%%20Maxwizard%20l=I%20need%20your%20assistance%20from%20dev). Don't forget to follow me on [Twitter](https://twitter.com/oladejoabdulla7) so that you don't miss any of my articles.