Select an entry from an array with a different value

I have a text file with the format (date, time, resistance):

12/11/2013 13:20:38 28.321930E+3 ... ... ... 

I need to extract the resistance value (third column) every 6 seconds after the first data entry. To start, I wanted to import a text file using:

 date, time, resistance = loadtxt('Thermometers.txt', unpack=True, usecols=[0,1,2]) 

However, before I almost started my program, I get an error message:

ValueError: invalid literal for float (): 12/12/2013

-Also -

I am not sure how to iterate over time given that the date changes as it runs overnight. Elegant solutions to my problem would be much appreciated.

+6
source share
2 answers

I think this code will do what you want to do. In addition, you do not need to worry about night data and changing the date, as this converts it to a datetime object.

  import datetime filtered_data=[] my_data=open(my_file,'r') for line in my_data: data_arr=line.split() dte=data_arr[0].split("/") r tme=data_arr[1].split(":") new_date=datetime.datetime((int(dte[2]),int(dte[0]),int(dte[1]), int(tme[0]),int(tme[1]),int(tme[2])) if filtered_data==[]: filtered_data.append(data_arr) else: if (new_date-old_date).seconds==6: filtered_data.append(data_arr) old_date=new_date 

This will give you a list in which items are filtered according to your situation (every 6 seconds). Now, if you just want your resistance array to be distributed at intervals of 6 seconds, just use a simple loop or list comprehension, as shown below:

 R_in_six_sec_interval=[R[2] for R in filtered_data] 
+1
source

you may want to look at this if you want to stick with numpy for other reasons.

0
source

Source: https://habr.com/ru/post/958072/


All Articles