np.loadtxt ignores the header, how can I save the header data?

Viewed 7198

I've saved an numpy array using savetxt and given the array a header. When I read the file using loadtxt, the header is ignored and only the data is saved in my new array. How can I access the header as it has important information I want to save as a string.

Edit:

np.savetxt(file_name, array, delimiter=",", header='x,y,z, data from monte carlo simulation')
data = np.loadtxt('test', dtype=float, delimiter=',')

I want to get "data from monte carlo simulation" and save it as a string.

1 Answers

To get the header you can simply read the first line of the file using .readline() method on your file. In your case It would look something like this :

f = open(filename)
header = f.readline()
last_col_name = header.split(',')[-1] #returns 'data from monte carlo simulation'

Also if you want to look into a more versatile way storing data you can check out the pandas library.

Related