[英]How to append CSV data with Google Sheets API and Python
[英]Push the csv file data to google sheets
我需要將 csv 文件數據加載到谷歌表。 我使用 gspread 將 csv 導入谷歌表格,但它只能與個人驅動器一起使用,而不能與團隊驅動器一起使用。
這是我的下一個方法。
我試圖將 csv 文件的內容放到列表中,然后從列表中更新谷歌表格的單元格並得到 429 錯誤。 我怎樣才能避免這個錯誤?
row = 1
column = 1
Fi = []
Fi = open("data.csv",'r')
for value in Fi:
items = value.strip().split(',')
for item in items:
sheet.update_cell(row, column, item)
column +=1
row +=1
column = 1
sheet.update_cells(cell_list)
Fi.close()
這是我得到的錯誤
gspread.exceptions.APIError: {
"error": {
"code": 429,
"message": "Quota exceeded for quota group 'WriteGroup' and limit 'Write requests per user per 100 seconds' of service 'sheets.googleapis.com' for consumer 'project_number:226307199654'.",
"status": "RESOURCE_EXHAUSTED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developer console API key",
"url": "https://console.developers.google.com/project/<proj_num>/apiui/credential"
}
]
}
]
}
}
我認為如果您跟蹤寫入請求並且僅根據每 100 秒分配的配額發送請求,則可以解決此問題。 並使用睡眠功能等待配額再過 100 秒重置。
import time
row = 1
column = 1
requestCount = 0
writeQuota = 100 #This is the limit for write request per 100 seconds
Fi = []
Fi = open("data.csv",'r')
for value in Fi:
items = value.strip().split(',')
for item in items:
if(requestCount == writeQuota):
time.sleep(100)
requestCount = 0
sheet.update_cell(row, column, item)
requestCount += 1
column += 1
row += 1
column = 1
sheet.update_cells(cell_list)
Fi.close()
嘗試檢查這樣的事情是否有效。 還可以調整睡眠時間並仔細檢查您的寫入配額。
每次請求完成時嘗試使用睡眠功能。 由於上面的錯誤,您需要在每個寫入請求完成后等待 100 秒以上。
你經常打電話給update_cells
。 根據此處的gspread
文檔
你可以批量完成:
cell_list = worksheet.range('A1:C7') for cell in cell_list: cell.value = 'O_o' # Update in batch worksheet.update_cells(cell_list)
所以,看起來你的解決方案是:
Fi = []
Fi = open("data.csv",'r')
items=[]
for value in Fi:
for item in value.strip().split(','):
items.append(item)
cell_list = worksheet.range('A1:ENDRANGE') # according to csv
for cell, item in cell_list, items:
cell.value=item
sheet.update_cells(cell_list)
Fi.close()
我沒有測試它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.