Actually it is very far from CSV.
You can use the file as an iterator; The following generator function gives full sections:
def load_sections(filename): with open(filename, 'r') as infile: line = '' while True: while not line.startswith('****'): line = next(infile)
This treats the file as an iterator, which means that any loop moves the file to the next line. The external circuit serves only to go from section to section; inner while and for loops do all the real work; first skip the lines until a **** section of the header is found (otherwise dropped), then loop through all the non-empty lines to create the section.
Use a function in a loop:
for section in load_sections(filename): print section
Repeating your sample data in a text file results in:
>>> for section in load_sections('/tmp/test.txt'): ... print section ... {'Data4': '715', 'Data1': '0.1834869385E-002', 'ID': '01', 'Data3': '-0.1091356549E+001', 'Data2': '10.9598489301'} {'Data4': '715', 'Data1': '0.1834869385E-002', 'ID': '01', 'Data3': '-0.1091356549E+001', 'Data2': '10.9598489301'} {'Data4': '715', 'Data1': '0.1834869385E-002', 'ID': '01', 'Data3': '-0.1091356549E+001', 'Data2': '10.9598489301'}
You can add some data converters to it if you want; mapping the key to the called will be:
converters = {'ID': int, 'Data1': float, 'Data2': float, 'Data3': float, 'Data4': int}
then in the generator function, instead of entry[key] = value do entry[key] = converters.get(key, lambda v: v)(value) .