I created a csv file with two columns, the first column is time data, and the second one is some measured data values.
2015/1/1 0:00 5
2015/1/1 0:15 10
2015/1/1 0:30 10
2015/1/1 0:45 15
2015/1/1 1:00 5
2015/1/1 1:15 20
2015/1/1 1:30 20
2015/1/1 1:45 40
2015/1/1 2:00 30
2015/1/1 2:15 20
2015/1/1 2:30 25
2015/1/1 2:45 10
2015/1/1 3:00
2015/1/1 3:15
2015/1/1 3:30
2015/1/1 3:45
2015/1/1 4:00
2015/1/1 4:15
2015/1/1 4:30 30
2015/1/1 4:45 50
2015/1/1 5:00 70
Now I want to use numpy.loadtxt function to read this two columns into two different numpy arrays with string data type for the date column and integer data type for the value column.
I tried different statements to do that, but none of them works.
time, data = np.loadtxt('TS.csv',dtype=str,delimiter=',',usecols=(0, 1),unpack=True)
time, data = np.loadtxt('TS.csv',dtype=(str,int),delimiter=',',usecols=(0, 1),unpack=True)
time, data = np.loadtxt('TS.csv',dtype=[str,int],delimiter=',',usecols=(0, 1),unpack=True)
Does anyone know how to realize the goal I just described? Thanks for your help!
You are very close to what you are looking for. Try this
data = np.loadtxt('TS.csv', dtype='str,int', delimiter=',', usecols=(0, 1), unpack=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With