我有一個QTableWidget,我將這個表中的數據導出到一個csv文件。 但現在,我想要打開一個現有的csv文件並使用此數據填充我的表格。我該怎麼做?? 這是我的出口代碼,我想要一個「填充」代碼,我真的不知道該怎麼做。我知道如何閱讀一個csv,但我不知道如何用這個csv數據填充我的表。pyqt - 用csv數據填充QTableWidget
高清輸出(個體經營):
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)
謝謝你雅各布。愛德華怎麼樣? :3 – Marco 2013-03-18 18:16:35
@psytron您應該引入您的備用代碼解決方案作爲新答案。閱讀[我應該何時編輯代碼?](https://meta.stackoverflow.com/q/260245/1677912) – Mogsdad 2018-01-04 17:53:37