Dec-24-2019, 01:03 PM
Don't give too much structures to the classes at first. The priority is to parse the file. You will only add the necessary code in the classes when you want to actually do something with the data. Here the simple key=value form of the input makes it easy to parse with regular expressions. See this example
import io import re SAP = io.StringIO('''\ Material=4000Psi Type=Concrete SymType=Isotropic TempDepend=No Color=Magenta Notes="Customary f'c 4000 psi 23/12/2019 2:17:43 pm" Material=A615Gr60 Type=Rebar SymType=Uniaxial TempDepend=No Color=White Notes="ASTM A615 Grade 60 23/12/2019 2:18:28 pm" Material=A992Fy50 Type=Steel SymType=Isotropic TempDepend=No Color=Red Notes="ASTM A992 Grade 50 23/12/2019 2:17:43 pm" Material=C30 Type=Concrete SymType=Isotropic TempDepend=No Color=Blue Notes="Concrete added 23/12/2019 2:18:37 pm" Material=C45 Type=Concrete SymType=Isotropic TempDepend=No Color=Blue Notes="Concrete added 23/12/2019 2:20:37 pm" Material=C60 Type=Concrete SymType=Isotropic TempDepend=No Color=Blue Notes="Concrete added 23/12/2019 2:21:13 pm" ''') class Statement: def __init__(self, data): self.data = data def __repr__(self): return "{}({})".format(self.__class__.__name__, self.data) class SAP_Material_01(Statement): pass parsed_file = [] for line in SAP: L = re.split(r'(\w+)[=]', line.strip()) assert L[0] == '' pairs = {} for i in range(1, len(L), 2): pairs[L[i]] = L[i+1].strip() item = SAP_Material_01(pairs) parsed_file.append(item) for item in parsed_file: print(item)
Output:SAP_Material_01({'TempDepend': 'No', 'Color': 'Magenta', 'SymType': 'Isotropic', 'Material': '4000Psi', 'Type': 'Concrete', 'Notes': '"Customary f\'c 4000 psi 23/12/2019 2:17:43 pm"'})
SAP_Material_01({'TempDepend': 'No', 'Color': 'White', 'SymType': 'Uniaxial', 'Material': 'A615Gr60', 'Type': 'Rebar', 'Notes': '"ASTM A615 Grade 60 23/12/2019 2:18:28 pm"'})
SAP_Material_01({'TempDepend': 'No', 'Color': 'Red', 'SymType': 'Isotropic', 'Material': 'A992Fy50', 'Type': 'Steel', 'Notes': '"ASTM A992 Grade 50 23/12/2019 2:17:43 pm"'})
SAP_Material_01({'TempDepend': 'No', 'Color': 'Blue', 'SymType': 'Isotropic', 'Material': 'C30', 'Type': 'Concrete', 'Notes': '"Concrete added 23/12/2019 2:18:37 pm"'})
SAP_Material_01({'TempDepend': 'No', 'Color': 'Blue', 'SymType': 'Isotropic', 'Material': 'C45', 'Type': 'Concrete', 'Notes': '"Concrete added 23/12/2019 2:20:37 pm"'})
SAP_Material_01({'TempDepend': 'No', 'Color': 'Blue', 'SymType': 'Isotropic', 'Material': 'C60', 'Type': 'Concrete', 'Notes': '"Concrete added 23/12/2019 2:21:13 pm"'})