我有这个python文件,将使用Google写入我的Google表格 使用此示例中的代码来完成表格API Python Google API writing to Google Sheet 谷歌片:
values = [
["Item", "Cost", "Stocked", "Ship Date",],
["Wheel", "20.50", "4", "3/1/2016",],
["Door", "15", "2", "3/15/2016",],
["Engine", "100", "1", "30/20/2016",],
["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)",],
]
Body = {
'values' : values,
'majorDimension' : 'ROWS',
}
}
result = service.spreadsheets().values().update(
spreadsheetId=spreadsheetID, range=rangeName,
valueInputOption='USER_ENTERED', body=Body).execute()
print("Writing OK!!")
但是我想编写一个如下所示的数据框:
Date ORCL TSLA IBM YELP MSFT
0 10/24/2016 37.665958 202.759995 145.080612 34.48 59.564964
1 10/25/2016 37.754536 202.339996 145.379303 33.950001 59.555199
2 10/26/2016 37.705326 202.240005 146.275406 33.490002 59.203667
3 10/27/2016 37.616749 204.009995 147.759277 32.740002 58.686134
4 10/28/2016 37.567539 199.970001 147.046234 32.290001 58.461548
5 10/31/2016 37.813587 197.729996 148.086884 32.66 58.510365
246 10/16/2017 48.860001 350.600006 146.830002 43.52 77.650002
247 10/17/2017 49.189999 355.75 146.539993 43.200001 77.589996
248 10/18/2017 49.580002 359.649994 159.529999 44.580002 77.610001
249 10/19/2017 49.349998 351.809998 160.899994 44.439999 77.910004
250 10/20/2017 49.25 345.100006 162.070007 44.52 78.809998
251 10/23/2017 49.310001 337.019989 159.550003 43.599998 78.830002
我从这里试过: Appending pandas Data Frame to Google spreadsheet 对谷歌的电子表格
import gspread
import gc
import pandas as pd
gc = gspread.authorize(credentials)
sh.share('otto@gmail.com', perm_type='user', role='writer')
sh = gc.open_by_key('1C09vB5F8zcyOrY4w_rctVUedXYJCZqtyoTc-bB0bgBY')
sheetName = 'sheet1'
rangeName = "Sheet1!A1:F252"
我宣布了我的数据框:
df = pd.DataFrame()
经过一些处理后,已创建非空数据框...和 填充它:
Date ORCL TSLA IBM YELP MSFT
0 10/24/2016 37.665958 202.759995 145.080612 34.48 59.564964
1 10/25/2016 37.754536 202.339996 145.379303 33.950001 59.555199
2 10/26/2016 37.705326 202.240005 146.275406 33.490002 59.203667
3 10/27/2016 37.616749 204.009995 147.759277 32.740002 58.686134
4 10/28/2016 37.567539 199.970001 147.046234 32.290001 58.461548
5 10/31/2016 37.813587 197.729996 148.086884 32.66 58.510365
246 10/16/2017 48.860001 350.600006 146.830002 43.52 77.650002
247 10/17/2017 49.189999 355.75 146.539993 43.200001 77.589996
248 10/18/2017 49.580002 359.649994 159.529999 44.580002 77.610001
249 10/19/2017 49.349998 351.809998 160.899994 44.439999 77.910004
250 10/20/2017 49.25 345.100006 162.070007 44.52 78.809998
251 10/23/2017 49.310001 337.019989 159.550003 43.599998 78.830002
# Output_conn = gc.open("SheetName").worksheet("xyz")
# Here 'SheetName' is google spreadsheet and 'xyz' is sheet in the workbook
Output_conn = gc.open(sheetName).worksheet(spreadsheetid)
for i,row in df.iterrows():
Output_conn.append_row(Row)
来自:Appending pandas Data Frame to Google spreadsheet frame-to-google-spreadsheet
我在这一行有错误信息:
Output_conn = gc.open(sheetName).worksheet(spreadsheetid)
AttributeError: 'module' object has no attribute 'open'
更新的代码:
import gspread
import gc
import pandas as pd
gc = gspread.authorize(credentials)
sh.share('otto@gmail.com', perm_type='user', role='writer')
sh = gc.open_by_key('1C09vB5F8zcyOrY4w_rctVUedXYJCZqtyoTc-bB0bgBY')
错误消息:UnboundedLocalError:在赋值之前引用的局部变量'sh'
有任何帮助吗?谢谢!