Reputation: 4062
I have the following code to iterate through a csv file. Iterate using the last column of in the csv file, column 11
I get the following error:
TypeError: next expected at least 1 arguments, got 0
The code is:
from operator import itemgetter
import os
import csv
reader = csv.reader(open('file1.csv','rb'))
#header = reader.next()
header = next().reader
data = list(reader)
reader = None
data.sort(key=itemgetter(11))
writer = csv.writer(open('2.csv', 'wb'))
writer.writerow(header)
writer.writerows(data)
I think it is complaining about the header.
Upvotes: 0
Views: 16291
Reputation: 1122102
You need to pass reader
as an argument to the next()
function:
header = next(reader)
or you could call the iterator.next()
method on the reader object:
header = reader.next()
I recommend sticking to the first spelling, especially since you can tell it to return None
(or another default) if the CSV file is empty:
header = next(reader, None)
Upvotes: 3