gpt4 book ai didi

python - pyqt - 使用 csv 数据填充 QTableWidget

转载 作者:行者123 更新时间:2023-12-02 01:04:24 32 4
gpt4 key购买 nike

我有一个 QTableWidget,我将数据从该表导出到 csv 文件。但现在,我想打开一个现有的 csv 文件并用这些数据填充我的表。我该怎么做?

这是我的导出代码,我想要一个“填充”代码,但我真的不知道该怎么做..我知道如何读取 csv,但我不知道如何使用此 csv 数据填充我的表。

def export(self):
nomeArquivo = "nomeArquivo"
filename = unicode(QFileDialog.getSaveFileName(self, "Document - Choose Export File", nomeArquivo+".csv"))
if not filename:
return
self.model.sort()
fh = None
try:

fh = QFile(filename)
if not fh.open(QIODevice.WriteOnly):
raise IOError, unicode(fh.errorString())
stream = QTextStream(fh)
stream.setCodec("UTF-8")
for row in range(self.model.rowCount()):
TSentence = self.model.data(
self.model.index(row, TABELA.SENTENCE)).toString()
TIrony = self.model.data(
self.model.index(row, TABELA.IRONY)).toString()

stream << "\""<< TSentence << "\"" << ";" << "\""<< TIrony <<"\"" <<"\n"

except (IOError, OSError), e:
QMessageBox.warning(self, "Text - Error",
"Failed to export: %s" % e)

finally:
if fh:
fh.close()
QMessageBox.warning(self, "Text - Export",
"Successfully exported text to %s" % filename)

最佳答案

看起来您可以在此处使用 csv 模块:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import csv

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QWidget):
def __init__(self, fileName, parent=None):
super(MyWindow, self).__init__(parent)
self.fileName = fileName

self.model = QtGui.QStandardItemModel(self)

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

self.pushButtonLoad = QtGui.QPushButton(self)
self.pushButtonLoad.setText("Load Csv File!")
self.pushButtonLoad.clicked.connect(self.on_pushButtonLoad_clicked)

self.pushButtonWrite = QtGui.QPushButton(self)
self.pushButtonWrite.setText("Write Csv File!")
self.pushButtonWrite.clicked.connect(self.on_pushButtonWrite_clicked)

self.layoutVertical = QtGui.QVBoxLayout(self)
self.layoutVertical.addWidget(self.tableView)
self.layoutVertical.addWidget(self.pushButtonLoad)
self.layoutVertical.addWidget(self.pushButtonWrite)

def loadCsv(self, fileName):
with open(fileName, "rb") as fileInput:
for row in csv.reader(fileInput):
items = [
QtGui.QStandardItem(field)
for field in row
]
self.model.appendRow(items)

def writeCsv(self, fileName):
with open(fileName, "wb") as fileOutput:
writer = csv.writer(fileOutput)
for rowNumber in range(self.model.rowCount()):
fields = [
self.model.data(
self.model.index(rowNumber, columnNumber),
QtCore.Qt.DisplayRole
)
for columnNumber in range(self.model.columnCount())
]
writer.writerow(fields)

@QtCore.pyqtSlot()
def on_pushButtonWrite_clicked(self):
self.writeCsv(self.fileName)

@QtCore.pyqtSlot()
def on_pushButtonLoad_clicked(self):
self.loadCsv(self.fileName)

if __name__ == "__main__":
import sys

app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')

main = MyWindow("/path/to/MyFile.csv")
main.show()

sys.exit(app.exec_())

PyQt5版本:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import csv

from PyQt5 import QtCore, QtGui, QtWidgets

class MyWindow(QtWidgets.QWidget):
def __init__(self, fileName, parent=None):
super(MyWindow, self).__init__(parent)
self.fileName = fileName

self.model = QtGui.QStandardItemModel(self)

self.tableView = QtWidgets.QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)

self.pushButtonLoad = QtWidgets.QPushButton(self)
self.pushButtonLoad.setText("Load Csv File!")
self.pushButtonLoad.clicked.connect(self.on_pushButtonLoad_clicked)

self.pushButtonWrite = QtWidgets.QPushButton(self)
self.pushButtonWrite.setText("Write Csv File!")
self.pushButtonWrite.clicked.connect(self.on_pushButtonWrite_clicked)

self.layoutVertical = QtWidgets.QVBoxLayout(self)
self.layoutVertical.addWidget(self.tableView)
self.layoutVertical.addWidget(self.pushButtonLoad)
self.layoutVertical.addWidget(self.pushButtonWrite)

def loadCsv(self, fileName):
with open(fileName, "r") as fileInput:
for row in csv.reader(fileInput):
items = [
QtGui.QStandardItem(field)
for field in row
]
self.model.appendRow(items)

def writeCsv(self, fileName):
with open(fileName, "w") as fileOutput:
writer = csv.writer(fileOutput)
for rowNumber in range(self.model.rowCount()):
fields = [
self.model.data(
self.model.index(rowNumber, columnNumber),
QtCore.Qt.DisplayRole
)
for columnNumber in range(self.model.columnCount())
]
writer.writerow(fields)

@QtCore.pyqtSlot()
def on_pushButtonWrite_clicked(self):
self.writeCsv(self.fileName)

@QtCore.pyqtSlot()
def on_pushButtonLoad_clicked(self):
self.loadCsv(self.fileName)

if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
app.setApplicationName('MyWindow')

main = MyWindow("data.csv")
main.show()

sys.exit(app.exec_())

关于python - pyqt - 使用 csv 数据填充 QTableWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15416663/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com