csv module

John Machin sjmachin at lexicon.net
Fri Aug 24 17:12:38 EDT 2007


On Aug 25, 12:29 am, luca72 <lucabe... at libero.it> wrote:
> Hello at all i have a problem with the csv module, really i don't
> undestud the writerows:
>
> if i have to write only one row i do as follow:
> def scrivo_csv(self):
>         import csv
>         q_righe = self.tableWidget.rowCount()
>         for row in range(q_righe) :
>             e_vuota = self.tableWidget.item(row, 5)
>             if e_vuota == None :
>                 ultima = row
>                 break
>         writerdocs = open("some.csv", "wb")
>         scritt = csv.writer(writerdocs, delimiter='|')
>         q_colonne = self.tableWidget.columnCount()
>         ss = 0
>         while ss < row :
>             riga = []
>             for val in range(q_colonne):
>                 esiste = self.tableWidget.item(ss, val)
>                 if esiste == None :
>                     luca = ''
>                 else :
>                     luca = str(QtGui.QTableWidgetItem.text(esiste))
>                 riga.append(luca)
>             scritt.writerow(riga)
>             ss+=1
> This work only with one row in the tablewidget, but if i have more
> than one row how i have to proceed?
>

The above code seems to be OK for "row" >= 2. What happens when there
are two or more rows?

Some style comments: "row" is a confusing name; I'd call it "nrows" or
something like that. "val" would be better as "column_index" or
"colx", "ss" as "row_index" or "rowx". Your while loop is better
expressed as a for loop.





More information about the Python-list mailing list