I would like to extract data from a txt file while removing the text present in the file using python.
I have a file, say ABC.txt as follows:
STEP = 1
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
STEP = 2
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
STEP = 3
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
22.530183726628522 0.0000000000000000
disregarding the 'STEP = ' and the following space, I want to store all the numeric data into a numpy array.
I tried the following script that worked :
import numpy as np
with open("ABC.txt", "r") as f:
lines = f.readlines()
data =np.zeros([24,2])
kk=0
for ii in range(3):
for jj in range(10*ii+2, 10*ii+9+1):
data[kk,:] = np.fromstring(lines[jj], dtype=float, sep=' ')
kk=kk+1
Is there a more direct way of doing this operation ?
splitand append to a list.np.array(alist, dtype=float)will convert the list of lists to a numeric array. Thesteplines can be ignored or used to start a new group.