首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过Python gspread从SQL数据库到Google sheet

通过Python gspread从SQL数据库到Google sheet
EN

Stack Overflow用户
提问于 2018-07-18 03:31:27
回答 1查看 1.3K关注 0票数 0

我正在尝试使用python和gspread从我的sql表中填充一个已经创建的google工作表。

我可以使用for循环一次一行地更新工作表,但我有很多数据要添加到工作表中,如果可能的话,我想一次更新一列或更多列。

这里的任何建议都是我一直在使用的,我得到了一个错误:'Row‘类型的对象不是JSON可序列化的

代码语言:javascript
复制
#!/usr/bin/python3
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import dbconnect

#credentials for google 
gc = gspread.authorize(credentials)


worksheet = gc.open('NAMEOFWS').sheet1
cell_list = worksheet.range('A2:A86')

#connect to database using dbconnect and grab cursor
query = "select loc from table"
cursor.execute(query)

results = cursor.fetchall()
cell_values = (results)

for i, val in enumerate(cell_values):  
    cell_list[i].value = val  
worksheet.update_cells(cell_list)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-18 04:29:38

我不确定如何使用gspread来做到这一点,但你可以很容易地修改你的代码,并使用pygsheet,它允许一次更新所有的列。此外,我不确定您的数据是什么样子,因此可能需要更改以下内容,或者您可能需要稍微更改您的数据集。希望这能有所帮助。

代码语言:javascript
复制
import pygsheets

gc = pygsheets.authorize(service_file = 'client_secret2.json')

# Open spreadsheet and select worksheet
sh = gc.open('Api_Test')
wks = sh.sheet1

#update Column
notes = [1,2,3,4] #this is the dataset to getupdated in the column
wks.update_col(4, notes, 1)# 4 is the number of column, notes is the dataset to update, 1 skips the first row (I used a header)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51388952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档