Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17234 > unrolled thread
| Started by | nukeymusic <nukeymusic@gmail.com> |
|---|---|
| First post | 2011-12-14 12:32 -0800 |
| Last post | 2011-12-15 08:07 +1100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | nukeymusic <nukeymusic@gmail.com> |
|---|---|
| Date | 2011-12-14 12:32 -0800 |
| Subject | replacing timestamps in file [newbie] |
| Message-ID | <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2011-12-14 12:56 -0800 |
| Message-ID | <22395461.354.1323896161063.JavaMail.geo-discussion-forums@prez15> |
| In reply to | #17234 |
You can either work with datetime module (see datetime.datetime.strptime) or with the time module (see time.strptime and time.mktime)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-12-15 08:07 +1100 |
| Message-ID | <mailman.3657.1323896833.27778.python-list@python.org> |
| In reply to | #17234 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web