Write to csv file without blank line in Python

In this article, we will learn how to write to a csv file without blank lines in Python.

Table Of Contents

Introduction to Problem

In Python, the writer() function of csv module controls the line endings. By default it writes the \r\n into the file directly. Therefore, in Python3 we must open file in untranslated text mode. It means, use the parameter newline=” with parameter ‘w’. Otherwise, it will write \r\r\n on Windows after each line. Let’s see an example to understand the issue,

import csv

# A List of lists
students = [['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('students.csv', 'w') as fileObj:
    writerObj = csv.writer(fileObj)
    writerObj.writerow(heading)
    writerObj.writerows(students)

It will create a csv file students.csv with four rows. If you will open this file on notepad, then it will look file. But if you will open this csv file in excel, then it will have a newline (empty line) after every row. Something like this,

Name    Age City    Country

Ritika  27  Delhi   India

Mark    28  Sydney  Australia

Suse    29  Las Vegas   USA

Shaun   30  London  UK

Solution

To write to csv without these extra newlines, we need to pass parameter newline=” in the opn() function. So, that newline after every row is controlled by csv writer only. Let’s see the solution,

Advertisements
import csv

# A List of lists
students = [['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('students2.csv', 'w', newline='') as fileObj:
    writerObj = csv.writer(fileObj)
    writerObj.writerow(heading)
    writerObj.writerows(students)

It will create a csv file without extra newlines. Contents of the file in excel will be,

Name,Age,City,Country
Ritika,27,Delhi,India
Mark,28,Sydney,Australia
Suse,29,Las Vegas,USA
Shaun,30,London,UK

Summary

We learned how to write a csv file with extra newlines in Python.

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