从 Google Drive 下载 csv 文件时遇到问题
Troubles downloading csv files from Google Drive
我想我没能理解我做错了什么。我已经连接到 Google 驱动器 API,现在我想阅读我的 Google 驱动器上的一些文档,并 objective 分析这些 csv 文件中包含的数据.
这是我的代码:
from __future__ import print_function
import pickle
import os.path
from httplib2 import Http
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/emmanuelsibanda/credentials.json"
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
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()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
def gdrive_download(file_id):
request = service.files().get(fileId=file_id)
return request.execute()
a = gdrive_download('19sPsANUblhXkb3nZVy4PMrfUyOSoF_uQ')
重申一下,我想下载几个 csv 文件,然后继续阅读这些文件。
在云端硬盘中,文件的元数据与其内容是分开的。您已要求 metadata.readonly
范围,不出所料,它只允许访问元数据。然后您对 files().get
的调用将获取元数据。
要获取内容,您首先需要选择一个更宽松的范围。然后你将获取内容。如何执行此操作取决于文件是二进制文件还是 Google 文档(例如电子表格)。
将alt=media
添加到URL可以下载二进制文件。
Google 文件可以通过将 /export?mimeType=text/csv
添加到 URL.
来导出
有关详细信息和代码示例,请参阅 https://developers.google.com/drive/api/v3/manage-downloads。
我想我没能理解我做错了什么。我已经连接到 Google 驱动器 API,现在我想阅读我的 Google 驱动器上的一些文档,并 objective 分析这些 csv 文件中包含的数据.
这是我的代码:
from __future__ import print_function
import pickle
import os.path
from httplib2 import Http
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/emmanuelsibanda/credentials.json"
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
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()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
def gdrive_download(file_id):
request = service.files().get(fileId=file_id)
return request.execute()
a = gdrive_download('19sPsANUblhXkb3nZVy4PMrfUyOSoF_uQ')
重申一下,我想下载几个 csv 文件,然后继续阅读这些文件。
在云端硬盘中,文件的元数据与其内容是分开的。您已要求 metadata.readonly
范围,不出所料,它只允许访问元数据。然后您对 files().get
的调用将获取元数据。
要获取内容,您首先需要选择一个更宽松的范围。然后你将获取内容。如何执行此操作取决于文件是二进制文件还是 Google 文档(例如电子表格)。
将alt=media
添加到URL可以下载二进制文件。
Google 文件可以通过将 /export?mimeType=text/csv
添加到 URL.
有关详细信息和代码示例,请参阅 https://developers.google.com/drive/api/v3/manage-downloads。