Reputation: 11
this is my google sheet doc https://docs.google.com/spreadsheets/d/1_cEg21VCXg8DfmCO45_314iwszmmqiC6GGh3eFxUcWs/edit?usp=sharing i need to detele the empty rows, i save all data in this way in python.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("pythonsheetsprue0.json", scope)
client = gspread.authorize(creds)
spreadsheet = client.open("Libro Diario").worksheet("Libro")
lista = spreadsheet.get_all_values()
i try this but it does nofor i in range(len(lista)): if lista[i-1][3] == " ": lista.remove(lista[i-1])
Upvotes: 1
Views: 709
Reputation: 440
Are you testing for a space with ' '
on purpose? if the row is truly empty the test should be''
.
Although in this particular spreadsheet you may be able to rely on the 3rd cell being empty to discard an entire row, here's one way to check the entire row before discarding it, makes it a little more robust.
lista = wks.get_all_values()
nrows = len(lista) + 1
i=0
while i < nrows-1:
try:
next(s for s in lista[i] if s)
i = i + 1
except StopIteration:
lista.remove(lista[i])
# to edit the spreadsheet itself, use delete_row()
wks.delete_row(i+1)
nrows -= 1
Upvotes: 1