[Python] I am unsure where the error keeps popping up

Discussão em 'Python' iniciado por Stack, Junho 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to automate updating of a google sheet using lists. Below is the latest iteration of my code

    import gspread
    import pandas as pd
    gc = gspread.service_account(filename="credentials.json")
    sh = gc.open('GCE MCQ')
    wks=['2024','2023','2022','2021','2020','2019','2018','2017','2016','2015','2014','2013','2012','2011','2010']
    lsts=list()
    t=open('list3.txt')
    for line in t:
    lists=lsts.append(line)
    x=0

    for wk in wks:
    worksheet = sh.worksheet(wk)
    body = [{


    "requests": [

    {"findReplace": {
    "find": str(x+1),
    "searchByRegex": True,
    "range": {
    "sheetId": worksheet.id,
    "startColumnIndex": 4,
    "endColumnIndex":5
    },
    "replacement":lsts[x]},}

    ]}

    ]
    while x<58:
    sh.batch_update(body)
    x=x+1


    It is unclear where the error keeps popping up from as using the above code without any lists, for/in and while works perfectly.

    Traceback (most recent call last):
    File "C:\Users\USER\p4ey\attempt2.py", line 33, in <module>
    sh.batch_update(body)
    ~~~~~~~~~~~~~~~^^^^^^
    File "C:\Users\USER\AppData\Local\Programs\Python\Python313\Lib\site-packages\gspread\spreadsheet.py", line 101, in batch_update
    return self.client.batch_update(self.id, body)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
    File "C:\Users\USER\AppData\Local\Programs\Python\Python313\Lib\site-packages\gspread\http_client.py", line 139, in batch_update
    r = self.request("post", SPREADSHEET_BATCH_UPDATE_URL % id, json=body)
    File "C:\Users\USER\AppData\Local\Programs\Python\Python313\Lib\site-packages\gspread\http_client.py", line 128, in request
    raise APIError(response)
    gspread.exceptions.APIError: APIError: [400]: Invalid JSON payload received. Unknown name "": Root element must be a message.

    Continue reading...

Compartilhe esta Página