Python Forum
Insert csv data rows into a tree widget
Thread Rating:
  • 3 Vote(s) - 2.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert csv data rows into a tree widget
#5
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:
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 used

.csv   somedwgs.csv (Size: 391 bytes / Downloads: 322)
Reply


Messages In This Thread
RE: Insert csv data rows into a tree widget - by Larz60+ - Nov-10-2017, 02:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 555 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987
  how do you style data frame that has empty rows. gsaray101 0 555 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 888 May-29-2023, 02:42 PM
Last Post: Nietzsche
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,287 Dec-19-2022, 11:11 PM
Last Post: Stockers
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,997 Dec-12-2022, 08:22 PM
Last Post: jh67
  How to insert different types of data into a function DrData82 0 1,274 Feb-10-2022, 10:41 PM
Last Post: DrData82
  How to read rainfall time series and insert missing data points MadsM 4 2,260 Jan-06-2022, 10:39 AM
Last Post: amdi40
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,660 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Iterating Through Data Frame Rows JoeDainton123 2 2,987 Aug-09-2021, 07:01 AM
Last Post: Pedroski55
Lightbulb [Solved] df.loc: write data in certain rows ju21878436312 1 1,751 Jun-28-2021, 06:49 AM
Last Post: ju21878436312

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020