2

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'
}
4
  • Show us a relevant sample of the CSV file. Use the csv module. Commented May 8, 2015 at 16:29
  • 1
    This might help: stackoverflow.com/questions/2868354/… Commented May 8, 2015 at 16:36
  • Hey Ziyao Wei - that definitely seems to get me closer but when I try csvfile.seek(0) I get AttributeError: DictReader instance has no attribute 'seek' (I use csv.DictReader to read my csv file) Commented May 8, 2015 at 16:42
  • 1
    @Mike: The seek function 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). Commented May 8, 2015 at 16:45

2 Answers 2

2

When you iterate the csvfile the first time, you exhaust it, so that it'll never yield more values.

One solution to your problem may be copying all the lines to a list, which can be iterated multiple times:

startTime = 1402617311
endTime = 1419544361
csv_lines = list(csvfile)   # <-
while startTime < endTime:
    for line in csv_lines:  # <-
        if (startTime + 30) > timeToEpoch(line['EnterTime']) >= endTime:
            output = calcFunction(line['EnterN'],line['ExitN'],line['user'])
    startTime = startTime + 30
Sign up to request clarification or add additional context in comments.

3 Comments

This got me really close - I made some tweaks which I'll show above because this made it iterate through item in the csv which made for a really long wait time
@Mike: you can also create (and accept) your own answer.
Good call - I added mine as well for clarity - but wanted to make sure that I gave you credit for getting me there! Thanks
0

So this was based on Andrea's suggestion I moved it to a list(), which worked great, but what I really needed to do was to check the condition every 30 seconds, which made it iterate through everything every 30 seconds (making it really slow) so I adapted it and slightly making it much quicker since all my times are sequential this works:

line = list(csv_data) #Convert into a list

lineCount = line[n] # Done so I can iterate line by line

while (startTime + 30) > timeToEpoch(lineCount['EnterN']) >= startTime:
    print "time on"
    pc = playerCount(lineCount['EnterN'],lineCount['ExitN'],lineCount['Player'])
    n += 1
    lineCount = line[n]

Note: sorry there were some small errors in my original code with endTime/startTime which didn't affect people's answers, but I changed in my final answer (some of this was because I renamed variables in an attempt to reduce confusion)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.