Table of Contents
CSV files or comma-separated files are used to store tabular data in files. In this article, we will discuss how we can read a CSV file line by line in Python.
Read CSV Line by Line Using the readline() Method in Python
A CSV file is created using commas and newline characters. Each row in a CSV file is separated using the newline character “\n”
and each entry in a row is separated by a comma. Following is a sample CSV file.
1 2 3 4 5 6 |
Roll Number,Name,Language 1,Aditya,Python 2,Arpit,Java 3,Harsh,C++ |
To read the CSV file line by line, we will use the readline()
method. The readline()
method, when invoked on a file object, returns the next line in the file that has not been read. To read the CSV file line by line we will first open the CSV file using the open()
method. After that, we can read each line in the file one by one as follows.
1 2 3 4 5 6 7 8 9 |
myFile = open('Demo.csv') print("The content of CSV file is:") text = myFile.readline() while text != "": print(text) text = myFile.readline() myFile.close() |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
The content of CSV file is: Roll Number,Name,Language 1,Aditya,Python 2,Arpit,Java 3,Harsh,C++ |
In the output above, you might observe that, after each line of the CSV file, an extra newline has been printed.
This is due to the reason that CSV files contain the newline character after each row. Also, the print()
function prints an extra newline character after printing each line. Due to this, two newline characters are printed. To avoid this, you can modify the behavior of the print()
function using the “end
” parameter.
The end
parameter takes a character as an input argument and prints it after printing the input value of the print()
function instead of the newline character. To avoid an extra newline character, we will pass an empty string to the “end
” parameter. In this way, the extra newline character will not be printed. You can observe this in the following example.
1 2 3 4 5 6 7 8 9 |
myFile = open('Demo.csv') print("The content of CSV file is:") text = myFile.readline() while text != "": print(text, end="") text = myFile.readline() myFile.close() |
Output:
1 2 3 4 5 6 7 |
The content of CSV file is: Roll Number,Name,Language 1,Aditya,Python 2,Arpit,Java 3,Harsh,C++ |
Alternatively, you can use the rstrip()
method to avoid the extra newline character when you read a CSV file line by line using the readline()
method in python. In this approach, we will remove the extra newline character from each line in the CSV file.
The rstrip()
method, when invoked on a string, removes the space character like newlines and tabs from the right side of the string and returns a modified string. We can print the modified string in place of the original line from the CSV file to avoid the extra newline as follows.
1 2 3 4 5 6 7 8 9 |
myFile = open('Demo.csv') print("The content of CSV file is:") text = myFile.readline() while text != "": print(text.rstrip()) text = myFile.readline() myFile.close() |
Output:
1 2 3 4 5 6 7 |
The content of CSV file is: Roll Number,Name,Language 1,Aditya,Python 2,Arpit,Java 3,Harsh,C++ |
If you don’t want to print the header of the CSV file, you can choose not to print the value after executing the readline()
method for the first time. In the subsequent executions, the rows containing the values will be printed as you can observe in the following example.
1 2 3 4 5 6 7 8 9 |
myFile = open('Demo.csv') print("The content of CSV file is:") text = myFile.readline() while text != "": text = myFile.readline() print(text.rstrip()) myFile.close() |
Output:
1 2 3 4 5 6 |
The content of CSV file is: 1,Aditya,Python 2,Arpit,Java 3,Harsh,C++ |
Read CSV Line by Line Using the readlines() Method in Python
Instead of using the readline()
method, we can use the readlines()
method to read a CSV file line by line. The readlines()
method, when invoked on a file object, returns a list of all the lines in the file.
To read the CSV file line by line, we will first open the file using the open()
method. After that, we will invoke the readlines()
method on the file object, which will return a list of all the lines in the CSV file. Then, we will iterate through the list and print all the lines one by one as follows.
1 2 3 4 5 6 7 8 |
myFile = open('Demo.csv') print("The content of CSV file is:") text_list = myFile.readlines() for line in text_list: print(line) myFile.close() |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
The content of CSV file is: Roll Number,Name,Language 1,Aditya,Python 2,Arpit,Java 3,Harsh,C++ |
Again, you can observe that an extra newline character has been printed after each line of the CSV file. You can avoid that using the “end
” parameter of the print()
function as follows.
1 2 3 4 5 6 7 8 |
myFile = open('Demo.csv') print("The content of CSV file is:") text_list = myFile.readlines() for line in text_list: print(line, end="") myFile.close() |
Output:
1 2 3 4 5 6 7 |
The content of CSV file is: Roll Number,Name,Language 1,Aditya,Python 2,Arpit,Java 3,Harsh,C++ |
Alternatively, you can use the rstrip()
method to avoid the extra newline character as shown below.
1 2 3 4 5 6 7 8 |
myFile = open('Demo.csv') print("The content of CSV file is:") text_list = myFile.readlines() for line in text_list: print(line.rstrip()) myFile.close() |
Output:
1 2 3 4 5 6 7 |
The content of CSV file is: Roll Number,Name,Language 1,Aditya,Python 2,Arpit,Java 3,Harsh,C++ |
To print the lines of the CSV file without the header, you can skip the first element of the list returned by the readlines()
method as follows.
1 2 3 4 5 6 7 8 9 |
myFile = open('Demo.csv') print("The content of CSV file is:") text_list = myFile.readlines() l = len(text_list) for i in range(1, l): print(text_list[i].rstrip()) myFile.close() |
Output:
1 2 3 4 5 6 |
The content of CSV file is: 1,Aditya,Python 2,Arpit,Java 3,Harsh,C++ |
Further reading:
Read CSV Line by Line Using CSV Module in Python
The CSV module provides us with various methods to read CSV files. To read a CSV file line by line in python, we can use the reader()
method and the DictReader()
method defined in the CSV module. Let us discuss them one by one.
Read CSV Line by Line Using the reader() Method
With the help of the reader()
method, we can read a CSV file line by line in python. The reader()
method, when invoked on a file object containing a CSV file, returns an iterator. On each iteration, the iterator returns a new line of the CSV file as a list where the elements of the list are the values in the respective row.
After opening the file and invoking the reader()
method on the file, we can iterate through the iterator to read the CSV file line by line as follows.
1 2 3 4 5 6 7 8 9 |
import csv myFile = open('Demo.csv') print("The content of CSV file is:") lines = csv.reader(myFile) for line in lines: print(line) myFile.close() |
Output:
1 2 3 4 5 6 7 |
The content of CSV file is: ['Roll Number', 'Name', 'Language'] ['1', 'Aditya', 'Python'] ['2', 'Arpit', 'Java'] ['3', 'Harsh', 'C++'] |
To print the lines of the CSV file without the header, you can skip the first value of the iterator using the next()
function. When we pass the iterator to the next()
function, it returns the value at which the iterator currently is. After that, the iterator is moved to the next element.
The header of the CSV file is kept at the first position in the iterator returned by the reader()
method. When we execute the next()
method once, with the iterator as the input value, the iterator moves to the second element. Thus, when we print a value from the iterator, the header is not printed. You can observe this in the following example.
1 2 3 4 5 6 7 8 9 10 11 12 |
import csv myFile = open('Demo.csv') print("The content of CSV file is:") lines = csv.reader(myFile) header = next(lines) for line in lines: print(line) myFile.close() |
Output:
1 2 3 4 5 6 |
The content of CSV file is: ['1', 'Aditya', 'Python'] ['2', 'Arpit', 'Java'] ['3', 'Harsh', 'C++'] |
Read CSV Line by Line Using the DictReader() Method
The DictReader()
method, when invoked on a file object containing a CSV file, returns a DictReader object. The DictReader is an iterator that contains each line of the CSV file as a dictionary. In the dictionary, each key is the column name and the associated value is the corresponding value from the CSV file in each line.
You can read each line of the CSV file line by line as a dictionary using the DictReader()
method as follows.
1 2 3 4 5 6 7 8 9 10 |
import csv myFile = open('Demo.csv') print("The content of CSV file is:") lines = csv.DictReader(myFile) for line in lines: print(line) myFile.close() |
Output:
1 2 3 4 5 6 |
The content of CSV file is: {'Roll Number': '1', 'Name': 'Aditya', 'Language': 'Python'} {'Roll Number': '2', 'Name': 'Arpit', 'Language': 'Java'} {'Roll Number': '3', 'Name': 'Harsh', 'Language': 'C++'} |
Conclusion
In this article, we have discussed four ways to read a CSV file line by line in python. You can choose any of the approaches at your convenience. All of the approaches have approximately the same time complexity and it all boils down to the format of the output as a deciding factor for choosing one approach to read any CSV file line by line.