繁体   English   中英

通过Gspread将.csv上传到Google表格

[英]Uploading .csv to Google Sheets via Gspread

我正在使用gspread用.CSV文件中的数据刷新Google表格中的工作表。 就我在网上浏览而言,我很难找到我认为应该是我的问题的明确答案。

我的项目目录中有.CSV文件。 到目前为止,这是我的代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import csv

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
sheet = client.open_by_key('Spreadsheet Key')
worksheet = sheet.worksheet('Worksheet Name')
worksheet.clear()

此时,我不确定应该使用哪种方法来批量上载.CSV中的所有数据。 我已经读过gspread中的.update_cells()只会使用一次对Google Sheets API的调用,并且是最快的方法,所以我的问题是这样的:

使用.update_cells(),如何遍历.CSV以能够发布到表格中?

关于.CSV文件的一些信息是它有9列,但是我需要代码来处理行数的任何更改。 任何帮助是极大的赞赏!

首先,如果可以接受第一个电子表格中进入A1的数据,则可以使用gspread.Client.import_csv()作为一个非常简单的选项。

否则,请继续阅读。

要使用update_cells() ,向其传递一个Cell()对象列表。

您可以从worksheet.range()worksheet.cell()worksheet.find()worksheet.findall()获得gspread对象。 (访问这些功能时,每个功能都会进行网络通话,因此请尽量减少通话次数。)

然后,对于每个单元格对象,将cell.value字段更改为csv数据。

for data,cell in zip(csv_data,worksheet.range()): # Whatever range you need.
    cell.value=data # Sets the cell in *local* sheet to the specified data. (String)

因此,这将更改该单元格的所有本地引用,以显示您的csv数据。 (值得指出的是,您的数据在上传时将转换为字符串。

如果您想将单元格数据读取为数字,我记得显示了cell.numeric_value ,尽管我在docs中没有看到任何引用。

然后,您将所有现在修改的单元格项目传递给update_cells() ,然后Google电子表格将反映您的更改。

您可以在此处查看其他参考资料: https : //github.com/burnash/gspread#updating-cells

另外 ,过去不得不解决一个非常相似的挑战(json,不是csv,但足够接近),这是一个很好的帮助程序函数,它将占用很多列,并完成获取单元格对象的繁重工作,然后发送update_cells()请求。 该代码可以在GitHub.com上找到

def update_columns(sheet, row, col, columns, execute = True):
    """Update the specified columns. Row and col are the starting most top left
       cell. Each column should be a list of values. Each list should be the
       same length.
    """
    # Step one, no columns is an error.
    if not columns:
        raise ValueError("Please specify at least one column to update.")

    # Otherwise, get that column length.
    r_len = len(columns[0])
    # First check that all columns are the same length.
    for column in columns[1:]:
        if len(column) != r_len:
            # Variable length.
            raise ValueError("Columns are of varying length.")

    # Start making lists.
    update_cells = []

    # Expand the sheet size if needed.
    if col + len(columns) > sheet.col_count:
        sheet.add_cols(col + len(columns) - sheet.col_count)

    if row + r_len > sheet.row_count:
       sheet.add_rows(row + r_len - sheet.row_count)

    # Get the range of cells to be updated.
    print("Range %s %s %s %s" % (row, col, row + r_len - 1 , col + len(columns) - 1))
    update_range = sheet.range (row, col, row + r_len - 1 , col + len(columns) - 1)

    for c, column in enumerate(columns):

        # Get the range on the sheet for the column.
##        column_range = sheet.range(row, col + c, row + len(column), col + c)
        column_range = (update_range[i] for i in range(c, len(update_range), len(columns)))

        for cell, value in zip(column_range, column):
            # Boolean rational.
            if isinstance(value, bool):
                if str(value).upper() != cell.value:
                    # So its NOT the same.
                    cell.value = value
                    update_cells.append(cell)

            # Use numerical_value for numbers.
            elif isinstance(value, (int, float)):
                # For whatever reason, it looks like gsheets
                # truncates to the 10th place.
                # It seems that 11th & 12th place is almost always correct but
                # can actually differ slightly???
                if cell.numeric_value is None or \
                   truncate(value, 10) != truncate(cell.numeric_value, 10):
                    cell.value = value
                    update_cells.append(cell)

            # And for everything else, string handling.
            elif isinstance(value, basestring):
                if value != cell.value:
                    cell.value = value
                    update_cells.append(cell)

            # Handle None
            elif value is None:
                if '' != cell.value:
                    # Set to ''
                    cell.value = ''
                    update_cells.append(cell)

            else:
                # Other type, error.
                raise ValueError("Cell value %r must be of type string, number, "
                                 "or boolean. Not %s." % (value, type(value)))

    # Now take the list of cells and call an update.
    if execute:
        print("Updating %d cells." % len(update_cells))
        if update_cells:
            sheet.update_cells(update_cells)
        return len(update_cells)
    else:
        return update_cells

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM