Venkata Narsi Reddy Vaddula Venkata Narsi Reddy Vaddula - 1 month ago 22
Python Question

Open a Datafile(csv,xls,xlsx,ods, etc) using "FILEPICKER" python?

I got to know how to open a data file when we know the name and type of the file but how do we code to pick a file with file picker?

import pyexcel as pe
records = pe.get_records(file_name="your_file.xls")
for record in records:
print("%s is aged at %d" % (record['Name'], record['Age']))

Answer

I got the problem, Now this code runs!

def loadCsv(self):


    self.progressBar.setValue(0)

    tab_table_view = QtGui.QWidget()
    self.Tab.insertTab(0, tab_table_view, self.File_Name)
    self.tableView = QtGui.QTableView(tab_table_view)
    self.tableView.setGeometry(QtCore.QRect(0, 0, 721, 571))

    self.model = QtGui.QStandardItemModel(self)
    self.tableView.setModel(self.model)
    self.tableView.horizontalHeader().setStretchLastSection(True)
    self.model.clear()


    file_name_temp=self.File_Name


    if (".csv" or ".txt") in self.File_Name:

        with open(file_name_temp, "rt") as fileInput:

            i = 1
            for row in csv.reader(fileInput):
                items = [
                    QtGui.QStandardItem(field)
                    for field in row
                    ]

                self.model.appendRow(items)
                # items.setFlags(QtCore.Qt.ItemIsUserCheckable |
                #               QtCore.Qt.ItemIsEnabled)
                # items.setCheckState(QtCore.Qt.Unchecked)
                Progress_completed = (i + 1) / (len(self.Datas)) * 100

                self.progressBar.setValue(Progress_completed)
                i += 1

    elif(".xls" or ".xml" or ".xlsx" or ".xlsm")in self.File_Name:
        import pandas as pd
        import numpy as np
        from itertools import chain
        ii = 1
        book=xlrd.open_workbook(self.File_Name)
        sheet = book.sheet_by_index(0)
        num_col = len(self.Datas.columns)
        num_row = len(self.Datas)
        colll = self.Datas.dtypes.index
        npData=np.array(self.Datas)
        # print(npData)
        col_names = np.array(colll)
        col_names = np.insert(col_names, 0, self.Datas.index.name)


        for i in range (num_row):
            # print (i)
            if i is 0:
                items = [QtGui.QStandardItem(col_names[j]) for j in range (num_col+1)]
            else:
                items = [QtGui.QStandardItem(str(self.Datas.index.values[i-1]))]+[ QtGui.QStandardItem(str(self.Datas.iat[i-1,j])) for j in range (num_col)]

            self.model.appendRow(items)

            Progress_completed = (ii + 1) / (len(self.Datas)) * 100

            self.progressBar.setValue(Progress_completed)
            ii += 1

    self.Tab.setCurrentIndex(0)
Comments