Pages

Tuesday, February 27, 2018

How to solve: '_csv.reader' object has no attribute 'next' : Python 3.6 CSV reader error

If you are trying to read a CSV file in Python 3.6 using this:

[python]
with open(filepath) as f:
reader = csv.reader(f, delimiter=’,’, quotechar=’"’, skipinitialspace=True)
header = next(reader)
# rest of the code
f.close()
[/python]

Then you might get the following (similar)error:

[shell]

Traceback (most recent call last):
File "<filepath>", line 17, in csv_to_json
header = reader.next()
AttributeError: ‘_csv.reader’ object has no attribute ‘next’

[/shell]

This is very simple error to solve.

[python]reader.next()[/python]

is actually Python 2.x syntax and that’s why it doesn’t work in Python 3.x. To make it work in Python 3.x, you should do:

[python]next(reader)[/python]

That’s it. Please comment if you face any issues.

No comments:

Post a Comment