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
#7
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
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 516 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987
  how do you style data frame that has empty rows. gsaray101 0 539 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 861 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,251 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,918 Dec-12-2022, 08:22 PM
Last Post: jh67
  How to insert different types of data into a function DrData82 0 1,262 Feb-10-2022, 10:41 PM
Last Post: DrData82
  How to read rainfall time series and insert missing data points MadsM 4 2,205 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,643 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Iterating Through Data Frame Rows JoeDainton123 2 2,959 Aug-09-2021, 07:01 AM
Last Post: Pedroski55
Lightbulb [Solved] df.loc: write data in certain rows ju21878436312 1 1,722 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