How to read a csv file after browsing it in PyQt5

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.

python-3.x
pandas
csv
dataframe
pyqt5
asked on Stack Overflow May 1, 2019 by vesuvius • edited May 1, 2019 by vesuvius

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0