How to Solve Python AttributeError: ‘_csv.reader’ object has no attribute ‘next’

by | Programming, Python, Tips

In Python 3, File object does not support the next() method. Instead, Python 3 has a built-in function next, which retrieves the next item from the iterator by invoking its __next__() method. If you try to call next() on a reader object in Python 3, you will raise the AttributeError: ‘_csv.reader’ object has no attribute ‘next.

To solve this error, use next(reader) instead.

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: ‘_csv.reader’ object has no attribute ‘next’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part of the error ‘_csv.reader’ object has no attribute ‘next‘ tells us that the reader object we are handling does not have the next method as an attribute. Python 3 no longer supports the next method.

Example

Let’s look at an example of opening a file using the csv module and reading the lines. First, we will look at the dataset:

pizza,price
margherita,£7.99
pepperoni,£8.99
four cheeses,£10.99
funghi,£8.99

Next, we will load the data into our program using the built-in open method, then create an iterator containing the lines in the file using csv.reader().

import csv

data = open('pizzas.csv', 'r')

reader = csv.reader(data)

The next step involves retrieving the items in the reader iterator using next(). We will call the next() method for times using a for loop.

for i in range(4):

   line = reader.next()

   print(line)

Let’s run the code to see the result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 1>()
      1 for i in range(5):
----> 2     line = reader.next()
      3     print(line)

AttributeError: '_csv.reader' object has no attribute 'next'

We get the AttributeError because next() is no longer supported in Python 3.

Solution

We need to use the built-in next() method instead of calling next() on the reader object to solve this error. Let’s look at the revised code:

import csv

data = open('pizzas.csv', 'r')

reader = csv.reader(data)

header = next(reader)

for i in range(4):

   line = next(reader)

   print(line)

We get the header by calling next(reader) once. Then, we will only print the lines with values in the for loop. Let’s run the code to see the result.

['margherita', '£7.99']
['pepperoni', '£8.99']
['four cheeses', '£10.99']
['funghi', '£8.99']

We successfully printed the lines to the console. Note that using the next() invokes the __next__() method of the iterator. Therefore we can also call the __next__ method on the iterator, though this is not common. Let’s look at the code:

import csv

data = open('pizzas.csv', 'r')

reader = csv.reader(data)

header = reader.__next__()

for i in range(4):

   line = reader.__next__()

   print(line)
['margherita', '£7.99']
['pepperoni', '£8.99']
['four cheeses', '£10.99']
['funghi', '£8.99']

Summary

Congratulations on reading to the end of this tutorial! The AttributeError ‘_csv.reader’ object has no attribute ‘next’ occurs when you try to call the next() method on a csv.reader object in Python 3. To solve this error, you need to call the built-in next() method and pass the reader object to it as a parameter.
To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨