GoogleSheetsAPI-Python-构建BatchUpdate的主体
我需要使用Python为Google电子表格的多个更新创建主体 我使用了Python字典GoogleSheetsAPI-Python-构建BatchUpdate的主体,python,api,dictionary,Python,Api,Dictionary,我需要使用Python为Google电子表格的多个更新创建主体 我使用了Python字典dict(),但这不适用于重复出现的多个值,因为dict()不允许多个键 我的代码片段是: body = { } for i in range (0,len(deltaListcolNames) ): rangeItem = deltaListcolNames[i] batch_input_value = deltaListcolVals[i]
dict()
,但这不适用于重复出现的多个值,因为dict()
不允许多个键
我的代码片段是:
body = {
}
for i in range (0,len(deltaListcolNames) ):
rangeItem = deltaListcolNames[i]
batch_input_value = deltaListcolVals[i]
body["range"] = rangeItem
body["majorDimension"] = "ROWS"
body["values"] = "[["+str(batch_input_value)+"]]"
batch_update_values_request_body = {
# How the input data should be interpreted.
'value_input_option': 'USER_ENTERED',
# The new values for the input sheet... to apply to the spreadsheet.
'data': [
dict(body)
]
}
print(batch_update_values_request_body)
request = service.spreadsheets().values().batchUpdate(
spreadsheetId=spreadsheetId,
body=batch_update_values_request_body)
response = request.execute()
查看文档:数据部分是dict对象的列表 因此,您的构造很接近,您只需要一个填充数据列表的循环:
data = []
for i in range(0, len(deltaListcolNames)):
body = {}
# fill out the body
rangeItem = deltaListcolNames[i]
....
# Add this update's body to the array with the other update bodies.
data.append(body)
# build the rest of the request
...
# send the request
...
谢谢你的回答,格雷厄姆。 我退了一步,不再使用dict范式,发现通过使用这个网格,我能够生成数据结构。这是我如何编码的。。。 也许有点奇怪,但它工作得很好:
range_value_data_list = []
width = 1
#
height = 1
for i in range (0,len(deltaListcolNames) ):
rangeItem = deltaListcolNames[i]
# print(" the value for rangeItem is : ", rangeItem)
batch_input_value = str(deltaListcolVals[i])
print(" the value for batch_input_value is : ", batch_input_value)
# construct the data structure for the value
grid = [[None] * width for i in range(height)]
grid[0][0] = batch_input_value
range_value_item_str = { 'range': rangeItem, 'values': (grid) }
range_value_data_list.append(range_value_item_str)