How to Browse Data file and extract data

37 Views Asked by At

i designed a desktop application using Qt designer and i want to take csv or excel file as input and display it in Qtable Widget. But data is not displayed in Table

def browse_data(self):
    def read_data_from_file(file_path):
        # the data is in a CSV format
        data = []
        with open(file_path, 'r') as file:
            for line in file:
                row = line.strip().split(',')
                data.append(row)
            return data
    options = QFileDialog.Options()
    file_path, _ = QFileDialog.getOpenFileName(self, "Open Data File", "", "CSV Files (*.csv);;All Files (*)",
                                               options=options)

    if file_path:
        data = read_data_from_file(file_path)
        self.populate_table(data)

def populate_table(self, data):
    self.tableWidget.setRowCount(len(data))
    for row_idx, row in enumerate(data):
        for col_idx, cell in enumerate(row):
            item = QTableWidgetItem(cell)
            self.tableWidget.setItem(row_idx, col_idx, item)
0

There are 0 best solutions below