replacing timestamps in file [newbie]

N

nukeymusic

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
Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00
Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+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:
0 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00
27 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00
54 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00

Is there a function in python to achieve this?

thanks in advance

nukey
 
M

Miki Tebeka

You can either work with datetime module (see datetime.datetime.strptime) or with the time module (see time.strptime and time.mktime)
 
C

Chris Angelico

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top