0

I am new to PyQt so excuse me if the question sounds like an amateur one. I am trying to read a csv file and then convert that file into a dataframe and merge it with another dataframe and print its result in the TableView. I am able to browse the specific file but when I click on Open , the file doesn't open and the application crashes with an error.

Process finished with exit code -1073740791 (0xC0000409)

This is the code I am using-

from PyQt5 import QtCore, QtGui, QtWidgets
import pandas as pd
import glob
import os
import csv

class Ui_Rule_Priority_test(object):
    def setupUi(self, Rule_Priority_test):
        Rule_Priority_test.setObjectName("Rule_Priority_test")
        Rule_Priority_test.resize(577, 531)
        self.gridLayout = QtWidgets.QGridLayout(Rule_Priority_test)
        self.gridLayout.setObjectName("gridLayout")
        self.OpenCsv = QtWidgets.QPushButton(Rule_Priority_test)
        self.OpenCsv.setObjectName("OpenCsv")
        self.gridLayout.addWidget(self.OpenCsv, 0, 0, 1, 1)
        self.OpenCsv.clicked.connect(self.file_open)
        self.tableView = QtWidgets.QTableView(Rule_Priority_test)
        self.tableView.setObjectName("tableView")
        self.gridLayout.addWidget(self.tableView, 1, 0, 1, 1)
        self.Refresh = QtWidgets.QPushButton(Rule_Priority_test)
        self.Refresh.setObjectName("Refresh")
        self.gridLayout.addWidget(self.Refresh, 2, 0, 1, 1)

        self.retranslateUi(Rule_Priority_test)
        self.Refresh.clicked.connect(self.tableView.clearSpans)
        QtCore.QMetaObject.connectSlotsByName(Rule_Priority_test)

    def retranslateUi(self, Rule_Priority_test):
        _translate = QtCore.QCoreApplication.translate
        Rule_Priority_test.setWindowTitle(_translate("Rule_Priority_test", "Dialog"))
        self.OpenCsv.setText(_translate("Rule_Priority_test", "Browse Csv and Get Score"))
        self.Refresh.setText(_translate("Rule_Priority_test", "Refresh"))


    def file_open(self):
        fileName = QtWidgets.QFileDialog.getOpenFileName(Rule_Priority_test, 'Open csv' , QtCore.QDir.rootPath() , 'Violations_*.csv')
        #df1 = pd.DataFrame()
        #df1 = pd.concat([pd.read_csv(f) for f in glob.glob('Violations_*.csv')] , ignore_index=True)
        path = self.lineEdit.text(fileName)
        df1 = pd.read_csv(path)
        df2 = pd.read_csv('C:\\Testing bat\\10000TXsData.csv')
        df = pd.merge(df2, df1, how='inner').dropna(axis="columns")
        model = PandasModel(df)
        self.tableView.setModel(model)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Rule_Priority_test = QtWidgets.QDialog()
    ui = Ui_Rule_Priority_test()
    ui.setupUi(Rule_Priority_test)
    Rule_Priority_test.show()
    sys.exit(app.exec_())

I am using a class PandasModel from this question How to display a Pandas data frame with PyQt5 which contains the MVC of a custom made model which helps us in getting the pandas dataframe into the code. If you could suggest something , it would be much appreciated.

This is how the csv looks like:

The first csv which I browse using fileName looks like this:

enter image description here

And the second csv which I read in df2 is:

enter image description here

I am trying to merge both these csv files as they have a common column of Rule ID.

vesuvius
  • 435
  • 4
  • 20
  • what is `PandasModel`? show the .csvs, provide a [mcve] – eyllanesc May 01 '19 at 11:36
  • Pandas Model is a class. I think this answer was written by you only @eyllanesc https://stackoverflow.com/questions/44603119/how-to-display-a-pandas-data-frame-with-pyqt5?noredirect=1&lq=1 . I copied the code of Pandas Model MVC from here. I will edit the question and add the image of the csv files. – vesuvius May 01 '19 at 11:46
  • I recommend you point out that the pandasModel you used from my old answer so that other users understand you – eyllanesc May 01 '19 at 11:48
  • I see that you are using self.lineEdit.text (fileName) but I do not see where you declared self.lineEdit – eyllanesc May 01 '19 at 11:53
  • Yeah it was a mistake. Can you suggest what I can do to read the fileName data and then convert it into a dataframe? – vesuvius May 01 '19 at 11:55
  • 1
    The solution is: remove 1) `path = self.lineEdit.text(fileName)` 2) change `fileName = QtWidgets.QFileDialog.getOpenFileName(Rule_Priority_test, 'Open csv' , QtCore.QDir.rootPath() , 'Violations_*.csv')` to `path, _ = QtWidgets.QFileDialog.getOpenFileName(Rule_Priority_test, 'Open csv' , QtCore.QDir.rootPath() , 'Violations_*.csv')` – eyllanesc May 01 '19 at 11:55
  • Thank you , it works. Can I ask you one more thing? After getting the tabular data , I am not able to clear the tableview , I am using a logic that if Refresh button is pressed , clearSpan() should clear the data but it is not happening. – vesuvius May 01 '19 at 12:00

0 Answers0