Start New Discussion within our Software Development Community

Hi,

I have a csv-file with Date in first column, pressure in second and temperature in third (a lot of data). Small example below:

08/08/2012 09:26:01,14.334,26.379
08/08/2012 09:26:02,14.329,26.376
08/08/2012 09:26:03,14.337,26.394
08/08/2012 09:26:04,14.324,26.421

I want to extract from t1 to t2 and then calculate the elapsed time (t2>t1). My code so far:

a=pd.read_csv(filename, parse_dates=True, keep_date_col=True)
t=a.get('Time')
P=a.get('P')
T=a.get('T-(C)')

T1 = '2012-8-8 10:10:10'
T2 = '2012-8-8 10:10:30'

t1=dt.datetime.strptime(T1, "%Y-%m-%d %H:%M:%S")
t2=dt.datetime.strptime(T2, "%Y-%m-%d %H:%M:%S")

Find elapsed time:

You could use something like this ...

'''pressure1.py
use seconds since epoch to calculate the elapsed time
'''

import time

# test data is csv type ...
# time,pressure,temperature
data = """\
08/08/2012 09:26:01,14.334,26.379
08/08/2012 09:26:02,14.329,26.376
08/08/2012 09:26:03,14.337,26.394
08/08/2012 09:26:04,14.324,26.421
"""

fname = "apressure.csv"

# write the test file
with open(fname, "w") as fout:
    fout.write(data)

# read the file back ...
# create a list of (seconds_since_epoch, pressure, temperature)
# tuples that you can then process
data_list = []
for line in open(fname):
    # strip trailing new line
    line = line.rstrip()
    mytime, pressure, temperature = line.split(",")
    #print(mytime, pressure, temperature)  # test
    time_tup = time.strptime(mytime, "%m/%d/%Y %H:%M:%S")
    # seconds since epoch 1/1/1970
    seconds = time.mktime(time_tup)
    #print(seconds)  # test
    data_list.append((seconds, float(pressure), float(temperature)))

# show ...
import pprint
pprint.pprint(data_list)

'''result ...
[(1344443161.0, 14.334, 26.379),
 (1344443162.0, 14.329, 26.376),
 (1344443163.0, 14.337, 26.394),
 (1344443164.0, 14.324, 26.421)]

'''
This question has already been answered. Start a new discussion instead.