无法在 gspread 中插入二维数组

Unable to insert 2d array within gspread

我正在尝试插入一个二维数组,以便通过 gspread 将两列插入到 sheet 中。我能够很好地插入单个列表,但插入数组会导致错误。这是我的代码。

def megaDepotScrape():
listings = 0
priceList = []
skuList = []
# Iterate through the listings on the page, printing the price per entry
for listings in range(0, 12):
    # Connect to the site to be scraped
    siteURL = "https://megadepot.com/catalog/lab-equipment/multiwell-plates/brand:brandtech/"
    response = requests.get(siteURL, headers=headers)   
    # with open('brandtech.html', 'wb') as fp:
    #     fp.write(response.content)
    # Cook the soup
    html_soup = BeautifulSoup(response.text, 'html.parser')
 
    # Find all containers with the appropriate class name
    # The 'strong' class 'hot' contains the price information
    price_containers = html_soup.find_all("strong", class_="hot")
    price = price_containers[listings]
    priceStr = list(price)
    priceList.append(priceStr)

    # Find all containers for the appropriate class name
    # The 'div' class 'product-wrapper' contains the SKU 
    sku_containers = html_soup.find_all("div", class_="product-wrapper")
    sku = sku_containers[listings]
    # The sku is stored in the 'data-variant' of the 'article' tag
    for data in sku.find_all("article"):
        skuData = data["data-variant"]
        skuList.append(skuData)

    # Iterate through the loop 
    listings += 1
    # Write both lists to the sheets document
    # Reference update() in docs
rows = [priceList, skuList]    
print(rows)
#sheet.update('A1', [list(e) for e in zip(*rows)])
sheet.insert_row(skuList)

这是行的值

[[['.57'], ['.91'], ['.63'], ['.63'], ['5.20'], ['6.90'], ['2.60'], ['6.10'], ['.22'], ['6.70'], ['7.30'], ['7.50']], ['781411', '781415', '781412', '781416', '701355', '701330', '701346', '701352', '782153', '701354', '781347', '781345']]

这是我得到的错误

    sheet.update('A1', [list(e) for e in zip(*rows)])      
  File "C:\Users\Jacob\PythonTestProject\venv\lib\site-packages\gspread\utils.py", line 592, in wrapper
    return f(*args, **kwargs)
  File "C:\Users\Jacob\PythonTestProject\venv\lib\site-packages\gspread\models.py", line 1127, in update
    {'values': values, 'majorDimension': kwargs['major_dimension']}
  File "C:\Users\Jacob\PythonTestProject\venv\lib\site-packages\gspread\models.py", line 236, in values_update        
    r = self.client.request('put', url, params=params, json=body)
  File "C:\Users\Jacob\PythonTestProject\venv\lib\site-packages\gspread\client.py", line 76, in request
    raise APIError(response)
gspread.exceptions.APIError: {'code': 400, 'message': 'Invalid values[0][0]: list_value {\n  values {\n    string_value: ".57"\n  }\n}\n', 'status': 'INVALID_ARGUMENT'}```

I'm not sure if there's some kind of limit that I'm hitting from uploading so much, or if there's some kind of error. Please let me know. Thank you.

我相信你的目标如下。

  • 来自以下示例值。

    Here's the value of rows

    [[['.57'], ['.91'], ['.63'], ['.63'], ['5.20'], ['6.90'], ['2.60'], ['6.10'], ['.22'], ['6.70'], ['7.30'], ['7.50']], ['781411', '781415', '781412', '781416', '701355', '701330', '701346', '701352', '782153', '701354', '781347', '781345']]

  • 我了解到priceListskuList的值可能是以下值

      priceList = [['.57'], ['.91'], ['.63'], ['.63'], ['5.20'], ['6.90'], ['2.60'], ['6.10'], ['.22'], ['6.70'], ['7.30'], ['7.50']]
      skuList = ['781411', '781415', '781412', '781416', '701355', '701330', '701346', '701352', '782153', '701354', '781347', '781345']
    
  • 您想将 priceListskuList 的值放入 2 列。

修改点:

  • 此时要求数组如下

      [["a1", "b1"], ["a2", "b2"],,,]
    
  • 当你想插入多行2列时,可以使用insert_rows().

当这反映到您的脚本中时,它变成如下。

示例脚本:

client = gspread.authorize(credentials)
spreadsheetId = "###" # Please set the Spreadsheet ID.
sheetName = "Sheet1" # Please set the sheet name you want to put the values.
spreadsheet = client.open_by_key(spreadsheetId)
sheet = spreadsheet.worksheet(sheetName)

# These values are from your question.
priceList = [['.57'], ['.91'], ['.63'], ['.63'], ['5.20'], ['6.90'], ['2.60'], ['6.10'], ['.22'], ['6.70'], ['7.30'], ['7.50']]
skuList = ['781411', '781415', '781412', '781416', '701355', '701330', '701346', '701352', '782153', '701354', '781347', '781345']

# I modified below script.
row = [[e1[0], e2] for e1, e2 in zip(priceList, skuList)]
print(row) # You can confirm the value of "row".
sheet.insert_rows(row)
  • 当您运行这个脚本时,priceListskuList的值被放入“Sheet1”的列“A”和“B”。

参考文献: