[Tutor] Help for Python Beginner with extracting and manipulating data from thousands of ASCII files
Oscar Benjamin
oscar.j.benjamin at gmail.com
Tue Oct 2 20:59:26 CEST 2012
Hi Cecilia, I'm sending this again as the first message was sent only
to you (I hadn't realised that your own message was sent only to me as
well). If you want to reply please reply-all to this message.
On 1 October 2012 17:42, Cecilia Chavana-Bryant
<cecilia.chavana at gmail.com> wrote:
> On Mon, Oct 1, 2012 at 11:02 AM, Oscar Benjamin <oscar.j.benjamin at gmail.com>
> wrote:
>>
>> On Sep 30, 2012 11:10 PM, "Cecilia Chavana-Bryant"
>> <cecilia.chavana at gmail.com> wrote:
>> >
>> > fileDate = data[6][16:26] # location of the creation date on the data
>> > files
>>
>> What format does fileDate have? I guess it's a string of text from the file. If
>> you can convert it to a datetime (or date) object it will be easy to
>> compare with the dates as required for your calibration file. Can you
>> show us how it looks e.g.
>>
>> '12-Nov-2012'
>> or
>> '12/11/12'
>> or something else?
>
>
> Date is in the following format: dd/mm/yyyy
The standard way to work with dates is to turn the date strings into
Python datetime objects. You can read about those here:
http://docs.python.org/library/datetime.html
datetime objects can be create directly:
>>> from datetime import datetime
>>> start_date = datetime(year=2012, month=11, day=3)
>>> print start_date
2012-11-03 00:00:00
You can also create them from a string:
>>> datestring = '10/11/2012'
>>> experiment_date = datetime.strptime(datestring, '%d/%m/%Y')
>>> print experiment_date
2012-11-10 00:00:00
Once you have two datetime objects you can compare them directly:
>>> experiment_date > start_date
True
>>> print experiment_date - start_date
7 days, 0:00:00
Using this you can check whether the date from the data file is in
between the start and end dates for each of the calibration files and
then choose which calibration file to use.
Oscar
More information about the Tutor
mailing list