【问题标题】:Append CSV file to Google Sheet将 CSV 文件附加到 Google 表格
【发布时间】:2021-09-16 06:19:04
【问题描述】:

我正在尝试将此 CSV 文件附加到此 Google 表格中的最后一行数据之后。但我只能覆盖现有数据。


import gspread
from oauth2client.service_account import ServiceAccountCredentials

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"]

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'key.json', scope)
client = gspread.authorize(credentials)

spreadsheet = client.open('upload_data')

with open('gmt2.csv', 'r') as file_obj:
    content = file_obj.read()
    client.import_csv(spreadsheet.id, data=content)

【问题讨论】:

    标签: python google-sheets google-sheets-api gspread


    【解决方案1】:

    使用import_csv 时,电子表格似乎被CSV 数据覆盖。我认为这可能是您的问题的原因。在您的情况下,使用append_rows 方法怎么样?当你的脚本被修改后,变成如下。

    修改脚本:

    client = gspread.authorize(credentials)
    
    # I modified below script.
    sheetName = "Sheet1" # Please set the sheet name you want to append the CSV data.
    spreadsheet = client.open('upload_data')
    worksheet = spreadsheet.worksheet(sheetName)
    content = list(csv.reader(open('gmt2.csv')))
    worksheet.append_rows(content, value_input_option="USER_ENTERED")
    
    • 在这种情况下,使用import csv。

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-11
      • 2019-09-09
      • 2022-12-07
      • 2020-08-30
      • 1970-01-01
      • 2014-05-13
      • 2021-07-21
      相关资源
      最近更新 更多