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,

Advertisements
  • 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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

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