无法将数据从 csv 传输到工作表

问题描述 投票:0回答:0
    files = {1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f', 7:'g'}
    COLUMNS = ['cs_mallId', 'counts']

    responseDf = pd.read_csv('/path/to/the/csv', usecols=COLUMNS)
    tokenPath='path/to/.token.json'
    scopes = ['https://www.googleapis.com/auth/spreadsheets',
            'https://www.googleapis.com/auth/drive']

    creds = service_account.Credentials.from_service_account_file(tokenPath)
    service = build('sheets', 'v4', credentials=creds)

    gc = pygsheets.authorize(service_account_file = tokenPath)
    gs = gc.open('name of Google sheet')
    sheet = gs.worksheet_by_title('name of worksheet')
    SPREADSHEET_ID = "worksheet ID"
    
    for mallId in files.keys():
        filteredDf = responseDf.loc[responseDf['cs_mallId'].isin(list(str(mallId)))]
        responseDf_countValues = filteredDf['counts'].tolist()
        responseDfRowCount = len(responseDf_countValues)
        print("responseDf_countValues : ", responseDf_countValues)

        row_offset = (mallId - 1) * 10
        range_name_resp = 'X' + str(5 + row_offset)  # for CSV file 1
        body = {
        'range': f'worksheet_name!{range_name_resp}',
        'values': responseDf_countValues,
        'majorDimension': 'ROWS'
        }
        print(body)
       
        response = service.spreadsheets().values().update(
        spreadsheetId=SPREADSHEET_ID,
        range=body['range'],
        valueInputOption='RAW',
        body=body,
        ).execute()

在这里,我试图将数据从我的 csv 添加到工作表的特定列。每当我执行我的代码时,它都会成功运行到“body”变量的末尾,但在最终向工作表发送响应时会抛出错误。所以,这就是我收到的错误消息:-

    Traceback (most recent call last):
    File "icrReports/icr_report_csv_to_sheets.py", line 374, in <module>
    icr_total_inv_categories(folderPath)
    File "icrReports/icr_report_csv_to_sheets.py", line 348, in   icr_total_inv_categories
    raise e
    File "icrReports/icr_report_csv_to_sheets.py", line 332, in

icr_total_inv_categories 响应 = service.spreadsheets().values().update( 文件“/home/kartik/OCR/icr-microservice/env/lib/python3.8/site-packages /googleapiclient/_helpers.py”,第 130 行,在 positional_wrapper 中 返回包装(*args,**kwargs) 执行文件“/home/kartik/OCR/icr-microservice/env/lib/python3.8/site-packages/googleapiclient/http.py”,第 938 行 提高 HttpError(resp,内容,uri=self.uri) googleapiclient.errors.HttpError: https://sheets.googleapis.com/v4/spreadsheets/1ywjmlMCao0w5WYHBeVsVcaTTbylfICXixH5zs8CABmw/values/Sheet57%21X5?valueInputOption=RAW&alt=json 返回“'data.values[0]' 处的无效值(类型.googleapis.com/google.protobuf.ListValue), 38 “data.values[1]”处的值无效 (type.googleapis.com/google.protobuf.ListValue),47 “data.values[2]”处的值无效 (type.googleapis.com/google.protobuf.ListValue),62 “data.values[3]”处的值无效 (type.googleapis.com/google.protobuf.ListValue),118 “data.values[4]”处的值无效 (type.googleapis.com/google.protobuf.ListValue),51 “data.values[5]”处的值无效 (type.googleapis.com/google.protobuf.ListValue),36 “data.values[6]”处的值无效 (type.googleapis.com/google.protobuf.ListValue),3".
详细信息:“[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'data.values[0]', 'description': “无效值在 'data.values[0]' (type.googleapis.com/google.protobuf.ListValue), 38"}, {'field': 'data.values[1]', 'description': "无效值 ' data.values[1]' (type.googleapis.com/google.protobuf.ListValue), 47"}, {'field': 'data.values[2]', 'description': 'data 处的无效值。 values[2]' (type.googleapis.com/google.protobuf.ListValue), 62"}, {'field': 'data.values[3]', 'description': 'data.values[ 处的无效值3]' (type.googleapis.com/google.protobuf.ListValue), 118"}, {'field': 'data.values[4]', 'description': 'data.values[4] 处的值无效' (type.googleapis.com/google.protobuf.ListValue), 51"}, {'field': 'data.values[5]', 'description': "'data.values[5]' 处的值无效 ( type.googleapis.com/google.protobuf.ListValue), 36"}, {'field': 'data.values[6]', 'description': "'data.values[6]' 处的值无效(类型。 googleapis.com/google.proto buf.ListValue), 3"}]}]">

请让我知道我在这里做错了什么。 谢谢。

python csv google-sheets google-api-python-client
© www.soinside.com 2019 - 2024. All rights reserved.