To read the whole file into one array:
out = np.genfromtxt('test.dat', skip_header=2, skip_footer=1, usecols=range(5))
To split it up into two arrays:
dist, c = np.split(out, [4], 1)
or maybe more simply:
dist = out[:, :4]
c = out[:, 4]
質問
I have the following data saved in a sample file test.dat:
n x y z c i1 i2
------------------------------------
1 1.2 3.4 5.6 1.0 111.1 222.2
2 7.8 9.0 1.2 2.0 333.3 444.4
3 3.4 5.6 7.8 1.0 555.5 666.6
------------------------------------
I do not need the last line.
I am trying to do the following steps:
I wrote the following code at the moment:
with open('test.dat', 'r') as f:
dump = f.readline().strip()
dump = f.readline().strip()
array = [ [float(x) for x in line.split()] for line in f]
f.closed
Please tell me how to do this using Python, and ndarray (as I want to do some numerical operation on the data afterwards.) Thank you!
解決
To read the whole file into one array:
out = np.genfromtxt('test.dat', skip_header=2, skip_footer=1, usecols=range(5))
To split it up into two arrays:
dist, c = np.split(out, [4], 1)
or maybe more simply:
dist = out[:, :4]
c = out[:, 4]