Skip to content Skip to sidebar Skip to footer

How Can I Load A CSV File Into A QTreeView?

Note: I am a complete beginner. I am using a pandas dataframe to import a csv file that I converted from the following table. I need to load the csv file into a QTreeView and have

Solution 1:

Below is a demo script which should do most of what you asked for. It cannot quite produce the same layout as in your second screenshot, but the structure is the same. The csv file is converted to nested dicts/lists, which can be saved to a json file. It's also possible to load the json file directly. I assumed your csv file looks like this:

"Test Category","Sub Category","Test Type","Scale Type"
"Premorbid Func.","SIMPLE","Actual","Scale"
"Premorbid Func.","SIMPLE","Predicted","Scale"
"Premorbid Func.","COMPL Montanist","TEST","Scale"
"Premorbid Func.","COMPL Montanist","Actual","Scale"
"Premorbid Func.","COMPL Montanist","Predicted","Scale"
"Intellect","WAIS-IV","WAIS-IV","T Score"
"Intellect","WAIS-IV","VCI","T Score"
"Intellect","WAIS-IV","Similarities","T Score"
"Intellect","WAIS-IV","Vocabulary","T Score"
"Attention","TOVA","RT","Scale"
"Attention","TOVA","RTV","Scale"
"Attention","DV","T","T Score"

Here is what the tree-view looks like:

enter image description here

Demo script:

import sys, os, csv, json
from collections import defaultdict
from PyQt5 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.buttonLoad = QtWidgets.QPushButton('Load Data')
        self.buttonLoad.clicked.connect(self.handleProcessData)
        self.buttonSave = QtWidgets.QPushButton('Save Data')
        self.buttonSave.clicked.connect(self.handleSaveData)
        self.buttonSave.setEnabled(False)
        self.tree = QtWidgets.QTreeView()
        layout = QtWidgets.QGridLayout(self)
        layout.addWidget(self.tree, 0, 0, 1, 2)
        layout.addWidget(self.buttonLoad, 1, 0)
        layout.addWidget(self.buttonSave, 1, 1)
        self.data = None

    def loadData(self):
        path, ok = QtWidgets.QFileDialog.getOpenFileName(
            self, 'Open CSV/JSON', '.', filter='Data Files (*.csv *.json)')
        if ok:
            with open(path) as stream:
                if os.path.splitext(path)[1] == '.json':
                    self.data = json.load(stream)
                else:
                    reader = csv.reader(stream)
                    # ignore the header
                    next(reader)
                    # convert to nested dicts/lists
                    self.data = defaultdict(lambda: defaultdict(list))
                    for record in reader:
                        self.data[record[0]][record[1]].append(record[2:])

    def handleProcessData(self):
        self.loadData()
        if self.data is not None:
            model = QtGui.QStandardItemModel(self.tree)
            model.setHorizontalHeaderLabels(('Category', 'Type', 'Scale'))
            self.tree.setModel(model)
            self.tree.setColumnWidth(0, 200)
            root = self.tree.rootIndex()
            for row, (text, values) in enumerate(self.data.items()):
                category = QtGui.QStandardItem(text)
                model.appendRow(category)
                self.tree.setFirstColumnSpanned(row, root, True)
                for row, (text, values) in enumerate(values.items()):
                    subcategory = QtGui.QStandardItem(text)
                    for value in values:
                        subcategory.appendRow([
                            QtGui.QStandardItem(),
                            QtGui.QStandardItem(value[0]),
                            QtGui.QStandardItem(value[1]),
                            ])
                    category.appendRow(subcategory)
                    self.tree.setFirstColumnSpanned(
                        row, category.index(), True)
            self.tree.expandAll()
            self.buttonSave.setEnabled(True)

    def handleSaveData(self):
        path, ok = QtWidgets.QFileDialog.getSaveFileName(
            self, 'Save JSON', '.', filter='JSON Files (*.json)')
        if ok:
            with open(path, 'w') as stream:
                json.dump(self.data, stream, indent=2)


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setWindowTitle('Test')
    window.setGeometry(600, 100, 540, 480)
    window.show()
    sys.exit(app.exec_())

Post a Comment for "How Can I Load A CSV File Into A QTreeView?"