But it's a simple process to manipulate the dictionary into any format that you need.
from pathlib import Path class TryRead: def __init__(self): self.header = None self.product_dict = {} self.file = Path('.') / 'somedwgs.csv' self.out_field_size = 22 def search(self, product_id): with self.file.open() as f: # skip first line (header) next(f) self.product_dict = {} product_found = False for line in f: line = line.strip().split('\t') if not product_found: if line[0] != product_id: continue product_found = True llen = len(line) if llen == 1: break elif llen == 5: product = line[0] part_desc = line[1] self.product_dict[product] = {} self.product_dict[product]['PartDesc'] = part_desc self.product_dict[product]['Drawings'] = {} self.product_dict[product]['Drawings'][line[2]] = {} self.product_dict[product]['Drawings'][line[2]]['Issues'] = line[3] self.product_dict[product]['Drawings'][line[2]]['Documents'] = line[4] else: self.product_dict[product]['Drawings'][line[0]] = {} self.product_dict[product]['Drawings'][line[0]]['Issues'] = line[1] self.product_dict[product]['Drawings'][line[0]]['Documents'] = line[2] # print(f'product_dict: {self.product_dict}') def generate_output(self): if not self.product_dict: print('Please select a product') else: for key, value in self.product_dict.items(): for skey, svalue in value['Drawings'].items(): dlen = self.out_field_size - len(skey) dlen2 = self.out_field_size - len(svalue['Issues']) outstr = f"{skey}{'.' * dlen}{svalue['Issues']}{'.' * dlen2}{svalue['Documents']}" yield outstr def show_formatted_product(self): for line in self.generate_output(): print(line) def show_current_product(self): if not self.product_dict: print('Please select a product') else: for key, value in self.product_dict.items(): print(f'Product: {key}') for skey, svalue in value['Drawings'].items(): print(f' {skey}') for ikey, ivalue in svalue.items(): print(f' {ikey}: {ivalue}') if __name__ == '__main__': tr = TryRead() tr.search('1-1841111-2') tr.show_current_product() print('') tr.show_formatted_product()output (sans the header):
Output:Product: 1-1841111-2
Dwg1
Issues: Iss A
Documents: c:\company\docs\tfpp\widget1.xls
Dwg2
Issues: Iss B
Documents: c:\company\docs\tfpp\bob.pdf
Dwg25
Issues: Iss Z
Documents: c:\company\docs\tfpp\itsatrap.pdf
Dwg1..................Iss A.................c:\company\docs\tfpp\widget1.xls
Dwg2..................Iss B.................c:\company\docs\tfpp\bob.pdf
Dwg25.................Iss Z.................c:\company\docs\tfpp\itsatrap.pdf