Reading Isochrones Data from Text Files in Python

This Python script demonstrates how to read and parse isochrone data from text files. It involves opening a file, reading its contents, and using regular expressions to identify and parse tables of isochrone data. The data is then loaded into pandas dataframes for each isochrone table found, making it easier to handle and analyze the numerical data contained within.

import pandas as pd
import numpy as np
import re
from io import StringIO
 
filename="isochrones_example.txt"
 
def read_iso(filename):
    with open(filename) as f:
        contents = f.read()
 
    lines = contents.split('\n')
 
    tabSep = r'((?:# number of EEPs, cols =\s+)(\d+)(?:\s+)(\d+))'
    tables = map(lambda x: (x[0], re.search(tabSep, x[1])), enumerate(lines))
    fTables = list(filter(lambda x: x[1], tables))
 
    sepTables = map(lambda x: lines[x[0][0]:x[1][0]], zip(fTables[:-1], fTables[1:]))
 
    isos = list()
    for table in sepTables:
        header = re.findall(r'[^\s\\#]+', table[2])
        okayTab = filter(lambda x: x!='' and x.lstrip()[0] != '#', table)
        strTab = '\n'.join(okayTab)
        df = pd.read_csv(StringIO(strTab), sep=r'\s+', names=header)
        isos.append(df)
    return isos