将Pandas DataFrames写入Google表格:没有这样的文件或目录.oauth / drive.json

时间:2016-09-01 14:51:16

标签: python-3.x oauth-2.0 google-sheets

我一直试图找到一种方法在Pandas和Google表之间读取和写入数据一段时间了。我发现图书馆df2gspread似乎非常适合这项工作。现在花了一段时间试图让它发挥作用。

根据说明,我使用Google API控制台创建了我的客户机密文件并将其另存为~/.gdrive_private。现在,我正在尝试下载Google电子表格的内容,如下所示:

workbook = [local filepath to workbook in Google Drive folder]
df = g2d.download(workbook, 'Sheet1', col_names = True, row_names = True)

当我运行此功能时,它会成功打开一个浏览器窗口,要求我的应用访问我的Google表格。但是,当我单击allow时,会出现一个iPython错误:

FileNotFoundError: [Errno 2] No such file or directory: '/Users/samlilienfeld/.oauth/drive.json'

这个文件应该包含什么?我已经尝试创建文件夹,并在那里再次包含我的客户端机密作为drive.json,但这不起作用。

3 个答案:

答案 0 :(得分:1)

我暂时通过将预先验证的凭据文件传递给g2d来进行解决。

我做了一个要点here(对于Python2x但应该工作3倍)通过传递秘密文件(基本上是〜/ .gdrive_private)和生成的经过身份验证的凭据文件名来保存凭证文件。

在具有适当文件名的独立脚本中使用上述要点,并从终端控制台运行它。将打开一个浏览器窗口以通过Google执行OAuth身份验证,并且应该为您提供一个令牌,您可以将其粘贴到终端提示中。这是一个简单的例子:

from gdrive_creds import create_creds


# Copy Paste whatever shows up in the browser in the console.
create_creds('./.gdrive_private', './authenticated_creds')

然后,您可以使用该文件对df2gspread调用进行身份验证。 使用gist方法创建cred文件后,尝试使用这样的方法来访问GDrive:

from oauth2client.file import Storage
from df2gspread import gspread2df as g2d


# Read the cred file
creds = Storage('./authenticated_creds').get()

# Pass it to g2df (Trimmed for brevity)
workbook = [local filepath to workbook in Google Drive folder]
df = g2d.download(workbook, 'Sheet1', col_names = True, credentials=creds)

df.head()

这对我有用。

答案 1 :(得分:0)

这个问题似乎是因为/User/***/.oauth文件夹不是由oauth2client包自动创建的(例如issue)。一种可能的解决方案是手动创建此文件夹,或者您可以更新df2gspread,问题应在最新版本中修复。

答案 2 :(得分:0)

以下是截至2019年的两种运行方式:

1.DateFrame数据到Google工作表:

#Import libraries 
import pandas as pd
import warnings
warnings.filterwarnings("ignore")

# Connection to googlesheet
import gspread
from oauth2client.service_account import ServiceAccountCredentials

# From dataframe to google sheet
from df2gspread import df2gspread as d2g

# Configure the connection 
scope = ['https://spreadsheets.google.com/feeds']

# Add the JSON file you downloaded from Google Cloud to your working directory
# the JSON file in this case is called 'service_account_gs.json' you can rename as you wish
credentials =ServiceAccountCredentials.from_json_keyfile_name('service_account_gs.json',
                                                           scope
                                                          )
# Authorise your Notebook with credentials just provided above
gc = gspread.authorize(credentials)

# The spreadsheet ID, you see it in the URL path of your google sheet
spreadsheet_key = '1yr6LwGQzdNnaonn....'

# Create the dataframe within your notebook
df = pd.DataFrame({'number': [1,2,3],'letter': ['a','b','c']})


# Set the sheet name you want to upload data to and the start cell where the upload data begins 
wks_name = 'Sheet1'
cell_of_start_df = 'A1'

# upload the dataframe
d2g.upload(df,
       spreadsheet_key,
       wks_name,
       credentials=credentials,
       col_names=True,
       row_names=False,
       start_cell = cell_of_start_df,
       clean=False)
print ('Successfully updated')

2.Google工作表到DataFrame

from df2gspread import gspread2df as g2d
df = g2d.download(gfile='1yr6LwGQzdNnaonn....',
              credentials=credentials,
              col_names=True, 
              row_names=False)
df