replacing timestamps in file [newbie]

Chris Angelico rosuav at gmail.com
Wed Dec 14 16:07:10 EST 2011


On Thu, Dec 15, 2011 at 7:32 AM, nukeymusic <nukeymusic at gmail.com> wrote:
> I have a file which has the data in the following format:
> Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00
>
> the first field is a timestamp, I'd like to replace it with the time
> in seconds starting from the first one like this:
> 27 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00

You're looking for several things here, all of which Python can do.
I'll give you a few pointers - have a browse of the documentation.

1) Reading a file line-by-line is usually best done with the iterator
idiom. Open your file with the "open()" function, read in any headers,
and then use "for line in inputfile:" to loop over the data.

2) Parse the line into separate pieces using the split() and join()
methods of the string object

3) Use the time.strptime() function to convert the string date into
something more usable

4) Maintain a variable with "previous time" and take the difference each time.

5) Write the modified lines to another file.

Python's pretty good with this sort of job. It won't let you down!

Chris Angelico



More information about the Python-list mailing list