csv module

L

luca72

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?

Thanks for the help

Luca
 
J

John Machin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top