I have a csv file that I'm trying to iterate through and have it do something only when a timestamp in it falls within a specified range. I'm using a while loop to do this where I'm adding 30 seconds each time through, but for some reason it only iterates through the csv the first time it does the loop and not each addition time (although I know it is successfully completing the while loop itself). I'm not sure what the problem is though.
try:
csvfilename = sys.argv[1]
except:
csvfilename = "myCSVfile.csv"
csvfile = csv.DictReader(open(csvfilename,'rb'),delimiter=',')
startTime = 1402617311
endTime = 1419544361
while startTime < endTime:
for line in csvfile:
if (startTime + 30) > timeToEpoch(line['EnterTime']) >= startTime:
output = calcFunction(line['EnterN'],line['ExitN'],line['user'])
startTime = startTime + 30
Per request here are a few lines of the csv file printed using the csv module (formatted a bit for readability, also the differences in the names and my example are because I changed them slightly):
{
'Time Exit': '17:02:04',
'Duration': '0:02:34',
'Date': '12/7/14',
'ExitN': '12/7/14 17:02',
'PlayerSpace': '1',
'EnterN': '12/7/14 16:59',
'Time Enter': '16:59:30'
}
{
'Time Exit': '17:08:18',
'Duration': '0:08:46',
'Date': '12/7/14',
'ExitN': '12/7/14 17:08',
'PlayerSpace': '2',
'EnterN': '12/7/14 16:59',
'Time Enter': '16:59:32'
}
{
'Time Exit': '17:06:49',
'Duration': '0:04:20',
'Date': '12/7/14',
'ExitN': '12/7/14 17:06',
'PlayerSpace': '3',
'EnterN': '12/7/14 17:02',
'Time Enter': '17:02:29'
}
csvmodule.csvfile.seek(0)I getAttributeError: DictReader instance has no attribute 'seek'(I usecsv.DictReaderto read my csv file)seekfunction need to be called on the file object rather than the reader - you can post some mode code if you still have problems (show how you define csvfile would be cool).