Uncategorized

numpy – delete unwanted characters from .dat file then doing calculation in python


I need to read .dat file in python which has 3 columns in total and hundreds of lines of rows. The columns include multiple characters (number and strings) .. So before I load that .dat file, do I need to delete all the other unwanted strings? …If not, how do I selectively declare the column and ask python to do the math?..
I already started with code.. seems working but includes a lot of for loops.. and I am quite new to python so I would appreciate any suggest with more easy operations..everytime.. I look here… seems only for strings..

an example of the .dat file would be serial_2.dat:

9:01:15 SA7.998 SC7.968
9:01:16 SA7.998 SC7.968

I have added the code I am using as a starter from your suggestion to remove character and create a new column to calculate average between column2 and column3:

import numpy as np
import os.path
from statistics import mean

time=[]
s_1=[]
s_2=[]
s1=[]
s2=[]
r1=[]
r2=[]
avgg=[]

# Reading data from file
with open('serial_2.dat','r') as f:
dat=f.readlines()      
for i in dat: 
     y=i.split()                          # cleaning and getting columns without spaces  
     time.append(y[0])
     s1.append(y[1])
     s2.append(y[2])
#getting only numbers without strings (SA and SC)           
for counter in (range(0,len(s1))):
    S_1=s1[counter]
    r1.append(S_1[2:])
    r1_f=np.array(r1, dtype="float32")  

   S_2=s2[counter]
   r2.append(S_2[2:])
   r2_f=np.array(r2, dtype="float32")  
   avgg=r1_f+r2_f/2
print(np.mean(avgg))

screenshot of serial_2.dat



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *