python - 通过 Python gspread 从 SQL 数据库到 Google 工作表
问题描述
我正在尝试使用 python 和 gspread 从我的 sql 表中填充已经创建的 google 表。
我可以使用 for 循环一次更新工作表一行,但我有很多数据要添加到工作表中,如果可能的话,我想一次或更多地做一列。
这里的任何建议都是我一直在使用的,我得到一个错误:“Row”类型的对象不是 JSON 可序列化的
#!/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)
解决方案
我不确定如何使用 gspread 执行此操作,但您可以非常轻松地修改代码并使用 pygsheets,它允许一次更新一列。另外,我不确定您的数据是什么样的,因此可能需要更改以下内容,或者您可能需要稍微更改您的数据集。希望这可以帮助。
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)
推荐阅读
- c - 将 memcmp 与 DOS 远指针一起使用
- javascript - 在列表中保留对所有孩子的参考是否有效?
- algorithm - 在Julia中迭代具有固定总和的整数数组
- html - 按钮在点击时跳跃
- django - Django外键和一对一访问最佳实践
- c++ - 如何将我的输出用于另一个方程?
- python - 如何输入数组 VALUE 作为参数而不是其 REFERENCE?
- python - 巨大的洞穴冒险文件:从二进制 (?) 解码为可读格式 (Python)
- vue.js - VueJS 使用 Mixin 数据初始化 Vue 属性
- python - 从一个 Dataframe 中选择 pandas 数据并通过在模式中选择行数据来创建不同的 n 个 Dataframe