How to Solve Python AttributeError: _csv.reader Object Has No Attribute Next

Rohan Timalsina Feb 02, 2024
How to Solve Python AttributeError: _csv.reader Object Has No Attribute Next

The CSV format is one of the most used formats in spreadsheets and databases. The Python language has the csv module that provides classes to read and write data in CSV format.

Attributes are values that are related to an object or a class. An AttributeError occurs in Python when you call an attribute of an object whose type is not supported by the method.

For instance, using the split() method on a file object returns an AttributeError because the file objects do not support the split() method.

This tutorial will teach you to fix the AttributeError: '_csv.reader' object has no attribute 'next' in Python.

Fix the AttributeError: '_csv.reader' object has no attribute 'next' Error in Python

The csv.reader object is an iterator. The next() method is available in the csv.reader object and returns the next row of the iterable object.

import csv

with open(csvfile) as f:
    reader = csv.reader(f, delimiter=",", quotechar='"', skipinitialspace=True)
    header = reader.next()
    f.close()

Output:

line 5, in <module>
    header = reader.next()
AttributeError: '_csv.reader' object has no attribute 'next'

But in Python 3, you have to use the built-in function next(reader) instead of the reader.next() method.

import csv

with open(csvfile) as f:
    reader = csv.reader(f, delimiter=",", quotechar='"', skipinitialspace=True)
    header = next(reader)
    f.close()

With this, the AttributeError should be solved in Python. We hope you found this article helpful.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - Python AttributeError

Related Article - Python Error