I have a text file that has content like this:
******** ENTRY 01 ********
ID: 01
Data1: 0.1834869385E-002
Data2: 10.9598489301
Data3: -0.1091356549E+001
Data4: 715
And then an empty line, and repeats more similar blocks, all of them with the same data fields.
I am porting to Python a C++ code, and a certain part gets the file line by line, detects the text title and then detect each field text to extract the data. This doesn't look like a smart code at all, and I think Python must have some library to parse data like this easily. After all, it almost look like a CSV!
Any idea for this?
It is very far from CSV, actually.
You can use the file as an iterator; the following generator function yields complete sections:
This treats the file as an iterator, meaning that any looping advances the file to the next line. The outer loop only serves to move from section to section; the inner
while
andfor
loops do all the real work; first skip lines until a****
header section is found (otherwise discarded), then loop over all non-empty lines to create a section.Use the function in a loop:
Repeating your sample data in a text file results in:
You can add some data converters to that if you want to; a mapping of key to callable would do:
then in the generator function, instead of
entry[key] = value
doentry[key] = converters.get(key, lambda v: v)(value)
.A very simple approach could be
my_file:
Python script:
Printed output: