Reputation: 902
I am new to Python (converting from Matlab) and I am having trouble reading a CSV file. Basically, I want create an array with the sequence of date/times in one variable and the corresponding data values (humidity measurements) in another variable (I want to skip the 2nd column). The file format is as follows:
07-24-14 01:00:01 PM,%RH,36.988
07-24-14 02:00:01 PM,%RH,40.832
...
I am using the numpy loadtxt function as follows (note: there are 21 headerlines in the file):
def datestr2num(s):
return datetime.strptime(s,'%m-%d-%y %I:%M:%S %p')
dates,vals = np.loadtxt('File.csv',usecols=(0,2),skiprows=21,converters={0:datestr2num},delimiter=',',unpack=True)
I am getting the following error:
TypeError: float() argument must be a string or a number
Thanks for your help in advance!
Upvotes: 1
Views: 1423
Reputation: 10781
An alternate approach would be to use a float time representation. I often use matplotlib time stamps:
from matplotlib.dates import date2num
def datestr2num(s):
return date2num(datetime.strptime(s, '%m-%d-%y %I:%M:%S %p'))
Or you could use the python built-in 'time':
import time
def datestr2num(s):
return time.mktime(datetime.strptime(s, '%m-%d-%y %I:%M:%S %p').timetuple())
If you'll be using one of these representations for your time anyway, this might work well. If you want datetimes, use dano's solution.
Upvotes: 1