Search code examples
pythonnumpydata-extractiongenfromtxt

Need help importing specific data (variable values) from a text file and ignoring non useful text and metadata


here is my attempted code though it may be rubbish and to the right is the data, i just want the two columns of data from line 15 onwards

my code reads:

import numpy as np 
import matplotlib as mplt 

data = np.genfromtxt('practice_data.txt', 
                     dtype='float', 
                     delimiter='  ') 
time = data[:,0]
channel=data[:,1]  

if anyone can help me to extract the two columns as two variables that would be amazing


Solution

  • With genfromtxt, you have a parameter which is named : skip_header. You can also extract the two columns as to variables like this :

    data = np.genfromtxt('pratice_data.txt', 
                        dtype=[('first column name','f8'),('second column name','i8')], 
                        delimiter=' ', 
                        skip_header = 14)
    

    skip_header = 14 let to pass the fourtheenth first lines.

    I think that with this way, you can passed header and you get two columns that you can call after ;)

    I didn't try the script, but it should work !

    Good luck ;)