【发布时间】:2021-11-23 01:15:48
【问题描述】:
这似乎是一项简单的任务,但我已经想了好几个星期了。 这很简单。我的本地驱动器上有一个 CSV 文件。我想将这些行附加到现有的 Google 表格中(已经有我需要保留的行) 代码如下:
import pandas as pd
import os.path
from googleapiclient import discovery
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from oauth2client.service_account import ServiceAccountCredentials
import gspread as gs
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"]
# Set variables
imp_dir = "C:\\Users\\SRevelle\\Google Drive"
credentials = ServiceAccountCredentials.from_json_keyfile_name('C:\\Users\\SRevelle\\Google Drive\\client_secret.json', scope)
client = gs.authorize(credentials)
service = discovery.build('sheets', 'v4', credentials=credentials)
#Go get the file to load
os.chdir(imp_dir)
csvData = 'data.csv'
#Load it into dataframe
df = pd.read_csv(csvData)
# The ID of the spreadsheet to update.
spreadsheet_id = '1_XXXXXXXXXXXdwEOkETjEI0HrdP5M5Jh4XXXXXXXXXX'
# Values will be appended after the last row of the table.
rangeval = 'Test!A1:H1'
# How the input data should be interpreted.
value_input_option = 'USER_ENTERED'
# How the input data should be inserted.
insert_data_option = 'INSERT_ROWS'
request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=rangeval, valueInputOption=value_input_option, insertDataOption=insert_data_option, body={'data':df})
response = request.execute()
这是来自 csv 文件的数据框结果:
Type Number Date Number Description1 Description2 CAD$ USD$
0 Visa 1211 2/9/2021 0 MISC PAYMENT RBC CREDIT CARD -20.88 0
1 MC 123 2/9/2021 0 testtest xyz card 101.11 0
我尝试了许多不同的方法,也经历了许多不同的错误。有时我使用 .to_json 只是为了看看会给我带来什么错误。
我使用此代码得到的错误是:
TypeError Traceback (most recent call last)
Untitled-2 in <module>
34
35 insert_data_option = 'INSERT_ROWS'
---> 36 request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=rangeval, valueInputOption=value_input_option, insertDataOption=insert_data_option, body={'data':df})
37 response = request.execute()
~\Miniconda3\lib\site-packages\googleapiclient\discovery.py in method(self, **kwargs)
1092
1093 headers = {}
-> 1094 headers, params, query, body = model.request(
1095 headers, actual_path_params, actual_query_params, body_value
1096 )
~\Miniconda3\lib\site-packages\googleapiclient\model.py in request(self, headers, path_params, query_params, body_value)
158 if body_value is not None:
159 headers["content-type"] = self.content_type
--> 160 body_value = self.serialize(body_value)
161 self._log_request(headers, path_params, query, body_value)
162 return (headers, path_params, query, body_value)
~\Miniconda3\lib\site-packages\googleapiclient\model.py in serialize(self, body_value)
271 ):
272 body_value = {"data": body_value}
--> 273 return json.dumps(body_value)
274
275 def deserialize(self, content):
我看到很多人尝试这样做,但解决方案是下载一些自定义应用程序,迭代循环文件或通过 GUI 导入它。一定有更简单的方法!!任何帮助将不胜感激。
【问题讨论】:
-
body参数必须是{"values": df},但我认为 Google 服务不能直接处理数据帧。您可能必须将其转换为列表列表。
标签: python pandas google-sheets google-sheets-api