I have a QTableWidget, i export the data from this table to a csv file. But now, i want to Open a existing csv file and populate my table with this data. how can i do it?? This is my export code, i want a "populate" code, and i really dont know how to do it.. i know how to read a csv but i dont know how to populate my table with this csv data.
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)
Looks like you could use the
csv
module here:This is from a project of mine: