I need to read some data from several text files that have random number lines of text at the beginning. Typically the files look like:
file1.dat
:
The file contains data
# this is a comment skip me
DataStart
index = integer
Some text
-5.0e-2 3.3 4.0
0 0.0e0 0.0e0
1.0 0.1 3.0
1.5 4.0 1.87
1.7 -4.67 0.124
...
...
15.3 -3.5e02 1.775
- At the beginning of
file1.dat
it may contain several lines of text that could start with spaces, tabs, etc. - The block of data I am interested in is always below those lines and has a fixed number of columns, in this case, it has 3 columns:
-5.0e-2 3.3 4.0
0 0.0e0 0.0e0
1.0 0.1 3.0
1.5 4.0 1.87
1.7 -4.67 0.124
...
...
15.3 -3.5e02 1.775
The lines containing the data could may have spaces/tabs at the start of each line.
I have tried the following code:
import numpy as np
pattern = r'^[-0-9 ]*'
mydata = np.fromregex('file1.dat', pattern, dtype=float)
But when I run it I get:
~/.local/lib/python3.8/site-packages/numpy/lib/npyio.py in fromregex(file, regexp, dtype, encoding)
1530 # Create the new array as a single data-type and then
1531 # re-interpret as a single-field structured array.
-> 1532 newdtype = np.dtype(dtype[dtype.names[0]])
1533 output = np.array(seq, dtype=newdtype)
1534 output.dtype = dtype
TypeError: 'NoneType' object is not subscriptable
Your help is very much appreciated