【问题标题】:Getting Google Sheets Data into Redshift将 Google 表格数据导入 Redshift
【发布时间】:2018-08-10 03:20:24
【问题描述】:

我正在尝试将 Google 表格中的数据导入我们的 Redshift 数据库。我能够按照此链接的指示进行操作:https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

是否可以让它从文件夹中最近添加的 google 工作表中提取数据(而不仅仅是指定单个工作表)并写入 Redshift 表?

以下是将谷歌表格数据读入 Python 的方法:

import gspread
from oauth2client.service_account import ServiceAccountCredentials


# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Copy of Legislators 2017").sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

【问题讨论】:

    标签: python google-sheets amazon-redshift google-api-python-client google-python-api


    【解决方案1】:

    您可以使用在给定时间范围内添加的特定类型的Drive APIquery for files。列出了此类查​​询的所有搜索参数和语法here

    # Build the Drive service
    ...
    
    # Query for recent files, with stipulation that their mimetype contains "spreadsheet"
    query = "mimeType contains 'spreadsheet' and modifiedTime > '"
    query += someDateAsUTC_inRFC_3339_String + "'"
    
    # Execute the query
    request = drive.files.list(q=query, .... )
    resp = request.execute()
    nextPage = resp['nextPageToken']
    if resp['files']:
        # Call method to consume files
    while nextPage:
        request = drive.files.list_next(request, resp)
        if request:
            resp = request.execute()
            nextPage = resp['nextPageToken']
            if resp['files']:
                # Call method to consume files
        else
            break
    # Done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-09
      • 2020-02-23
      • 1970-01-01
      • 2023-01-01
      • 2016-06-23
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      相关资源
      最近更新 更多