Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17237

Re: replacing timestamps in file [newbie]

References <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com>
Date 2011-12-15 08:07 +1100
Subject Re: replacing timestamps in file [newbie]
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3657.1323896833.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Dec 15, 2011 at 7:32 AM, nukeymusic <nukeymusic@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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

replacing timestamps in file [newbie] nukeymusic <nukeymusic@gmail.com> - 2011-12-14 12:32 -0800
  Re: replacing timestamps in file [newbie] Miki Tebeka <miki.tebeka@gmail.com> - 2011-12-14 12:56 -0800
  Re: replacing timestamps in file [newbie] Chris Angelico <rosuav@gmail.com> - 2011-12-15 08:07 +1100

csiph-web