Write rows to csv file line by line in Python

In this article, we will discuss different ways to write rows to a csv file in Python, and that too line by line.

Table Of Contents

Write multiple lists to csv file line by line

Suppose we have 5 different lists i.e.

heading = ['Name', 'Age', 'City', 'Country']
row1 = ['Ritika', 27, 'Delhi', 'India']
row2 = ['Mark', 28, 'Sydney', 'Australia']
row3 = ['Suse', 29, 'Las Vegas', 'USA']
row4 = ['Shaun', 30, 'London', 'UK']

Now we want to add these lists into a csv file. Where, first list should be added as a header row, and the remaining four lists should be added as normal rows.

To do that we need to use the csv module. Steps are as follows,

  • Open the file in write mode, and get a file object.
  • Pass the file object to writer() function of csv module, to get the csv_writer object.
  • Now call the writerow() function of csv writer object five times with different lists.
  • If will add different rows to csv file, line by line.

Let’s see an example,

import csv

# A list containing the heading of csv file
heading = ['Name', 'Age', 'City', 'Country']

row1 = ['Ritika', 27, 'Delhi', 'India']
row2 = ['Mark', 28, 'Sydney', 'Australia']
row3 = ['Suse', 29, 'Las Vegas', 'USA']
row4 = ['Shaun', 30, 'London', 'UK']

with open('employees.csv', 'w') as fileObj:
    writerObj = csv.writer(fileObj)
    writerObj.writerow(heading)
    writerObj.writerow(row1)
    writerObj.writerow(row2)
    writerObj.writerow(row3)
    writerObj.writerow(row4)

Here, we added one header row, and four normal rows to a csv file.

Write list of lists to a csv file line by line

Suppose we have a list of lists,

employees = [['Ritika', 27, 'Delhi', 'India'],
             ['Mark', 28, 'Sydney', 'Australia'],
             ['Suse', 29, 'Las Vegas', 'USA'],
             ['Shaun', 30, 'London', 'UK']]

Now we want to add all sublists in this list into a csv file as different rows. To do that we need to use the csv module. Steps are as follows,

  • Open the file in write mode, and get a file object.
  • Pass the file object to writer() function of csv module, to get the csv_writer object.
  • Pass the first list to the writerow() function of csv writer object. It will add a header row in the csv file.
  • Iterate over all the lists in main list, and for each sub list call the writerow() function of csv writer object.
  • If will add different rows to csv file, line by line.

Let’s see an example,

import csv

# A List of lists
employees = [['Ritika', 27, 'Delhi', 'India'],
             ['Mark', 28, 'Sydney', 'Australia'],
             ['Suse', 29, 'Las Vegas', 'USA'],
             ['Shaun', 30, 'London', 'UK']]

# A list containing the heading of csv file
heading = ['Name', 'Age', 'City', 'Country']

with open('employees2.csv', 'w') as fileObj:
    writerObj = csv.writer(fileObj)
    writerObj.writerow(heading)
    for row in employees:
        writerObj.writerow(row)

Here, we added one header row, and four normal rows to a csv file.

Summary

We learned about different ways to write rows to a csv file line by line.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top