I am using a csv iterator to go through a csv file, which contains data associated with time. I am sure the csv file is correct. I am using Jupyter, iPython notebook in python 3.x
When I try to iterate on the first row by using .next() method, I have an AttributeError: 'module' object has no attribute 'next'.
My code is divided in two parts, one part containing function and imports, one part calling them. The function I have a problem with is:
def get_durations(csvfile):
try:
csvfile = iter(csvfile)
except TypeError, te:
print csvfile, 'is not iterable'
print "data is", repr(csvfile)
first_time = csvfile.next()[5]
first_time = (first_time.replace(" ", ""));
for row in csvfile:
last_time = row[5]
last_time = (last_time.replace(" ", ""))
first_time = datetime.datetime.strptime(first_time, "%H:%M:%S")
last_time = datetime.datetime.strptime(last_time, "%H:%M:%S")
return first_time.replace(second = 0), last_time.replace(second = 0)
I make the call to the function here:
for el_mousefile in mousefiles:
os.chdir(el_mousefile)
print "data is", repr(csvfile)
csvfile = csv.reader(open("mouse.csv", "rU"), delimiter=';', quoting=csv.QUOTE_NONE)
print "data is", repr(csvfile)
try:
csvfile = iter(csvfile)
except TypeError, te:
print csvfile, 'is not iterable'
first_time, last_time = get_durations(csv)
I get this output when trying to run the program:
data is <_csv.reader object at 0x000000000A388D08>
data is <_csv.reader object at 0x000000000A388948>
module 'csv' from 'C:\Users\**\AppData\Local\Continuum\Anaconda\lib\csv.pyc' is not iterable
data is module 'csv' from 'C:\Users\**\AppData\Local\Continuum\Anaconda\lib\csv.pyc'
96------>first_time = csvfile.next()[5]
97 first_time = (first_time.replace(" ", ""));
98 for row in csvfile:
AttributeError: 'module' object has no attribute 'next'
I don't understand how can my csv be iterable in the second part, but then when being passed to the function, it is not iterable anymore which is responsible for my error.
first_time, last_time = get_durations(csv)
You are passing in the csv module, not a file.