Ok,
I had my dinner.
here's how I would read this file.
The code would have been more compact if the structure were the same from line to line.
Since you are using tkinter, you obviously won't be using the show_current_product function,
but I put it there to show how to access the dictionary.
In addition, I wrote this so that the dictionary could handle multiple products with minor modifications.
Here's the code:
somedwgs.csv (Size: 391 bytes / Downloads: 322)
I had my dinner.
here's how I would read this file.
The code would have been more compact if the structure were the same from line to line.
Since you are using tkinter, you obviously won't be using the show_current_product function,
but I put it there to show how to access the dictionary.
In addition, I wrote this so that the dictionary could handle multiple products with minor modifications.
Here's the code:
from pathlib import Path class TryRead: def __init__(self): self.header = None self.product_dict = {} self.file = Path('.') / 'somedwgs.csv' 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 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()and the results:
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
Just in case there are any file differences, attached is a copy of what I usedsomedwgs.csv (Size: 391 bytes / Downloads: 322)