f = open('iii.log', 'r') q = {} for line in f.readlines(): parts = line.split(" ") if (parts[4] == "(2031)"): # threadid, barcode, time # print parts[3], parts[10], parts[1] q[(parts[3], parts[10])] = parts[1] if (parts[4] == "(2032)"): #print q[(parts[3], parts[10])], parts[1] then = q[(parts[3], parts[10])] now = parts[1] then = then.replace(":", ".") now = now.replace(":", ".") then = then.split(".") now = now.split(".") then = (int(then[0]) * 3600) + (int(then[1]) * 60) + int(then[2]) now = (int(now[0]) * 3600) + (int(now[1]) * 60) + int(now[2]) print now - then del q[(parts[3], parts[10])] #print "interleaved: %d" % (len(q))