Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'function,': 0.07; 'python': 0.08; 'pointers': 0.09; 'received:209.85.160.174': 0.09; 'received:mail-gy0-f174.google.com': 0.09; 'am,': 0.12; 'subject:file': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterator': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'modified': 0.18; 'convert': 0.19; 'seconds': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'pieces': 0.23; 'string': 0.24; "python's": 0.24; 'function': 0.27; 'variable': 0.28; 'do.': 0.28; 'separate': 0.28; 'message-id:@mail.gmail.com': 0.28; 'lines': 0.30; 'format:': 0.30; 'usable': 0.30; 'chris': 0.30; 'usually': 0.31; "i'll": 0.31; 'thu,': 0.32; 'subject:]': 0.32; 'pretty': 0.32; 'sort': 0.33; "won't": 0.33; 'received:209.85.160': 0.33; 'object': 0.33; 'done': 0.34; 'to:addr:python-list': 0.34; 'loop': 0.34; 'file.': 0.34; 'things': 0.34; 'parse': 0.34; 'here,': 0.35; 'something': 0.35; 'file': 0.36; 'starting': 0.36; 'data.': 0.36; 'received:google.com': 0.37; 'another': 0.37; 'open': 0.38; 'using': 0.38; 'replace': 0.38; 'several': 0.38; 'received:209.85': 0.38; "i'd": 0.39; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'difference': 0.40; 'more': 0.61; '2011': 0.61; 'your': 0.61; 'skip:+ 10': 0.66; '"for': 0.67; 'time"': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=NC/lw0KEapsI/XVUfE5HsK7GHK9KkVjpgGW/beY5nkY=; b=EYrgQCipOiwvFF0G1gsqC8jB9wkd7/+7eeI0TocQYDzubX5V2WjHMIyfcAHOPG6sxd GwKwlINKpWbRDKLVLwOI+i4Vsrs5x/+hk5CZNa9DONf8PAp3XavwkFDOh8OtiWryMp/s XQiazhEVjWvnVXR0Pz5wBc2H42uQjQzrcu4p8= MIME-Version: 1.0 In-Reply-To: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> References: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> Date: Thu, 15 Dec 2011 08:07:10 +1100 Subject: Re: replacing timestamps in file [newbie] From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323896833 news.xs4all.nl 6864 [2001:888:2000:d::a6]:43573 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17237 On Thu, Dec 15, 2011 at 7:32 AM, nukeymusic 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