0

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!

Robert
  • 652
  • 2
  • 11
  • 23
  • I would say the best way is to require data in some reasonable format; text + binary in one file? are you going to open it with rb? or you just call text binary data because it represents binary numbers? – Drako May 30 '17 at 12:29
  • well, unfortunately I cannot change the data format. I didn't get your second idea. What do you mean by text binary data? – Robert May 30 '17 at 12:31
  • I mean you can open file with "r" as text file or "rb" as binary file – Drako May 30 '17 at 12:32
  • can you show an example of desired output? – Mohd May 30 '17 at 12:35
  • if its all text, just do with open(file) as f: for line in f: if '.dataset' in line: ... (save info from next lines in vars until again .dataset or EOF) – Drako May 30 '17 at 12:35
  • What's the desired output that you are looking for? What kind of structure you want to make out of this? – Ishan Bhatt May 30 '17 at 12:39
  • @Moe A: Again - it's complicated. I need to find blocks where one param is 1. These are references. If the param is 0, it is data and I extrapolate any value of 0-blocks with a mean of all 1-block values... – Robert May 30 '17 at 12:52
  • @Drako : I read them as rb until now. – Robert May 30 '17 at 12:54

0 Answers0