【问题标题】:Display a large csv file in PyQt Table View在 PyQt 表视图中显示一个大的 csv 文件
【发布时间】:2014-06-23 20:38:47
【问题描述】:

我试图在 PyQt 应用程序中显示 NTFS 卷的主文件表。我已提取 MFT 并转换为 csv 文件,现在我希望使用 PyQt Table View 以表格形式显示数据。程序运行完美,没有任何错误,但什么也不显示。

CSV 文件的大小为 300 Mb。

现在这就是我的代码的样子:

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(640, 480)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.tableView = QtGui.QTableView(self.centralwidget)
        self.tableView.setObjectName(_fromUtf8("tableView"))
        self.verticalLayout.addWidget(self.tableView)
        MainWindow.setCentralWidget(self.centralwidget)

        self.actionOpen = QtGui.QAction(MainWindow)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/icons/open.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.actionOpen.setIcon(icon)
        self.actionOpen.setObjectName(_fromUtf8("actionOpen"))


        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.actionExit, QtCore.SIGNAL(_fromUtf8("triggered(bool)")), MainWindow.close)
        QtCore.QObject.connect(self.actionOpen, QtCore.SIGNAL("triggered()"),self.ExtractMFT)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.actionOpen.setText(QtGui.QApplication.translate("MainWindow", "Open", None, QtGui.QApplication.UnicodeUTF8))


    def ExtractMFT(self, root = None):
        if root == None:
            root = "\\\\.\C:"

        FileName = "MFT-EXtracted"
        CSVName = "MFT-EXtracted.csv"
        print 1
        Control=subprocess.Popen(["icat",root,"0-128-1",">",FileName],shell =True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        Control.wait()
        print 2
        print Control.stdout.read()
        if Control.stderr == None:
            print 3
            #Control=subprocess.Popen(["python","analyzeMFT.py","-f",FileName,"-o",CSVName],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
            #Control.wait()
            print 5
            print Control.stdout.read()
            if Control.stderr == None:
                self.loadCsv(CSVName)


    def loadCsv(self, fileName):
        # The problem persist in this function. The items being appended to the 
        # model are not being displayed by the tableView , infact the tableView 
        # is empty nothing come's up. 
        header = False
        header_data=[]
        data=[]
        print 6
        se2 = 0
        model = QtGui.QStandardItemModel()
        with open(fileName, "rb") as fileInput:
            for row in csv.reader(fileInput):
                if header == True:
                    items = [
                    QtGui.QStandardItem(field)
                    for field in row
                ]
                    model.appendRow(items)
                elif header ==False:
                    for field in row:
                        items = [
                        field
                        for field in row
                        ]
                        header_data.append(items)
                     header == True

           print 7
           self.tableView.setModel(model)

def main():
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow() # <-- Instantiate QMainWindow object.
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec_()



main()

我打算加载的 CSV 文件中有 300,000 多行,因此它们是一种将数据加载到视图中的有效方法。从而使用更少的系统资源。

【问题讨论】:

  • 您能缩小问题的范围,还是我们应该为您调试?通过明智地使用打印语句,您可能可以告诉我们代码应该在哪里做某事但不是。
  • @Schollii :我已经编辑了代码并删除了不相关的代码。
  • 在这里查看我的答案:stackoverflow.com/questions/31588584/…

标签: python csv pyqt qtableview qstandarditemmodel


【解决方案1】:

您很可能只想加载用户可以看到的文件部分(在两边加上一点点,以便您确定何时加载更多内容)。您应该能够应用Big Tables page 中讨论的技术。您可以通过 google 找到其他类似的教程或讨论。

【讨论】:

  • @rene 看起来 qt 文档已经移动了。我已经更新了链接。
  • 感谢@schollii 的快速回复!干杯
猜你喜欢
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
相关资源
最近更新 更多