更新Google Drive上的CSV文件,以便使用PyDrive在Data Studio上使用



我需要更新驱动器上的CSV文件,因为我在Google Data Studio的面板上使用它。直到现在,我一直在使用这个代码:

previous_GDS_df = pd.read_excel(path_to_GDS_file)
pd.concat(objs=[previous_GDS_df, df_GDS]).to_excel(path_to_GDS_file, index=False)
f = drive.CreateFile({'id': spreadsheet_id})
f.SetContentFile(path_to_GDS_file)
f.Upload()

其中:

  • "previous_GDS_df";是我正在更新的CSV文件的内容
  • "quot;path_to_GDS_file";我在其上进行修改的本地CSV文件的路径
  • "df_GDS";我修改的df,我想附加到Drive上的文件中的元素

基本上,我的理论如下:"我提取文件的前一个内容,将新内容附加到文件中,然后用"SetContentFile"编辑驱动器文件,并将其全部上传">

问题是,当我在Drive上编辑我的文件时,每次我的文件在仪表板GDS中时,我都需要重新连接,因为我认为SetContentFile会完全擦除以前的文件Drive来写一个新文件。在这种情况下,我必须将驱动器文件重新连接到GDS,因为它已被删除和重写。

所以,我正在寻找一个解决方案来更新我的驱动器文件,这样我就不必每次将我的文件重新连接到dahsboard,修改就会神奇地出现。

你有解决方案吗?我的理论肯定是错误的。我在某个地方错过了什么。

谢谢你的帮助,如果需要更多信息,请问我。

---编辑---我测试了一些解决方案,但没有任何效果。我测试过的最好的解决方案是这个(感谢评论中的帮助(:

from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from apiclient.http import MediaFileUpload
SCOPES = ['https://www.googleapis.com/auth/drive'] # If modifying these scopes, delete the file token.json.
def getCreds(): # Authentication

creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())

return creds

def updateFile(service, spreadsheet_id, path_to_GDS_file): # Call the API
media = MediaFileUpload(path_to_GDS_file, mimetype='application/vnd.google-apps.spSreadsheet', resumable=True)
res = service.files().update(fileId=spreadsheet_id,media_body=media,fields="*").execute()
return res
def main(spreadsheet_id, path_to_GDS_file):
creds = getCreds()
service = build('drive', 'v3', credentials=creds)
updateFile(service, spreadsheet_id, path_to_GDS_file)
if __name__ == '__main__':
main()

但是main((函数不仅仅是附加到我在GDrive上的csv文件,它重写了整个文件,所以我必须在Data Studio上重新连接。

你知道我如何将行附加到位于GDrive上的csv文件中吗?

谢谢。

我对pydrive了解不多,但为了通过Drive API更新文件,您必须使用Files:update。这使您既可以更新文件元数据,也可以更新文件内容。

下面是一个可能使用官方Python库的示例:

from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from apiclient.http import MediaFileUpload
SCOPES = ['https://www.googleapis.com/auth/drive'] # If modifying these scopes, delete the file token.json.
fileId = "DRIVE_FILE_ID" # Change to yours
filePath = "LOCAL_FILE_PATH" # Change to yours
def getCreds(): # Authentication
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return creds
def updateFile(service, fileId): # Call the API
media = MediaFileUpload(filePath, mimetype='text/csv', resumable=True)
res = service.files().update(fileId=fileId,media_body=media,fields="*").execute()
return res
def main():
creds = getCreds()
service = build('drive', 'v3', credentials=creds)
updateFile(service, fileId)
if __name__ == '__main__':
main()

注:

您首先必须下载您的凭据文件,如下面引用的快速启动中所述。

参考:

  • Python快速启动
  • service.files((.update((

最新更新