calculate difference between two timestamps [newbie]

Peter Otten __peter__ at web.de
Sun Dec 18 10:01:25 EST 2011


nukeymusic wrote:

> thanks and also thanks to all the others who were so kind to help me
> out with my first python-script.
> I tested your alternatives and they work, the only a minor
> inconvenience is that the first line of the inputfile gets lost i.e.
> the first timestamp should become zero (seconds)

That should be easy to fix:

>> with open('testfile','r') as f, open('outputfile','w') as g:
>>      first_date, first_rest = parse_line(next(f))
        g.write("0 %s" % first_rest)
>>      for line in f:
>>          cur_date, rest = parse_line(line)
>>          delta = cur_date - first_date
>>          g.write("%s %s" % (int(round(delta.total_seconds())), rest))

 





More information about the Python-list mailing list