I want to read a quite complex file with python, where several blocks of data are to be analysed. The file looks somehow like that:
.dataset 'hello'
.param
param1 123
param2 456
.data bin
text1
text2
ndata 256
<here comes a chunk of 256 (number behind ndata) binary numbers>.dataset 'hello'
.param
param1 124
param2 235
.data bin
text1
text2
ndata 256
<here comes a chunk of 256 binary numbers>.dataset 'hello'
.param ...
So as a more detailed description: the dataset starts with the keyword ".dataset" and its type ('hello').
Then I get the params and their values in the given form after the keyword ".params". Then I get the keyword ".data bin" with two lines of information, one line where the number of bin values is given and the binary data themselves. Then it begins again.
The params do change from time to time (not the amount of params, but their values), ndata is always the same.
My question is:
- What is the best way to iterate through the file, notice the keywords and react accordingly (in .params:
infile.readlines(...)
or something similar; in .data bin: read the whole chunk until the next ".dataset" is reached)
In fact I know how to do the operations seperately but not how to go through the file and do it after a certain keyword. Especially since I cannot read the file line-wise. Would appreciate any help!