简体   繁体   English

python gspread从响应主体更新多个单元格

[英]python gspread updating multiple cells from reponse body

I am using this python script to take a response from Progresso API: http://docs.progresso.apiary.io/#reference/behaviour/behaviour-events-collection/get-behaviour-events 我正在使用此python脚本从Progresso API进行响应: http : //docs.progresso.apiary.io/#reference/behaviour/behaviour-events-collection/get-behaviour-events

from urllib2 import Request, urlopen
import smtplib import gspread 
from oauth2client.service_account import ServiceAccountCredentialseaders = {
'Authorization': 'Bearer [CURRENT_TOKEN]'
}
request = Request('https://private-anon-ae5edf57e7-progresso.apiary-
mock.com/BMEvents/?Behaviour=new', headers=headers)
response_body = urlopen(request).read()
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('ProgressoAPI-
2f6ecaa6635c.json', scope)
gc = gspread.authorize(credentials)
wks  = gc.open("Progresso Test").sheet1
wks.clear()
cell_list = wks.range('A1:H20')
for cell in cell_list:
cell.value = response_body
wks.update_cells(cell_list)

I know the cell.value = response body is wrong and I don't know how I can get it right - I am stuck. 我知道cell.value =响应主体是错误的,而且我不知道如何正确处理-我被卡住了。

it appears in every cell like this: "{ ""BehaviourEntryId"": 13798177, ""LearnerId"": 245277, ""LearnerCode"": ""2009-0080"", ""RegGroup"": ""U6-RWE"", ""Behaviour"": ""Negative"", ""IncidentDate"": ""2017-02-07"", ""Subject"": ""BE"", ""Location"": ""CLS"", ""Published"": ""Yes"", ""Creator"": ""DhDr"", ""Editor"": null, ""Assignee"": ""DiRo"", ""Status"": ""Completed"", ""Details"": [ { ""Category"": ""CL"", ""Type"": ""CLatt"", ""Severity"": ""S2"", ""point"": 0 }, { ""Category"": ""CL"", ""Type"": ""CLBEH"", ""Severity"": ""S2"", ""point"": 2 } ], ""Comments"": [ { ""BehaviourEntryCommentId"": 5648278, ""Confidential"": true, ""Comment"": ""Asked to go to the toilet and went to the one furthest away just to waste time."" }, { ""BehaviourEntryCommentId"": 5648279, ""Confidential"": false, ""Comment"": ""Spat gum out on floor"" }, { ""BehaviourEntryCommentId"": 5648280, ""Confidential"": false, ""Comment"": ""Was rude to memeber of Staff"" } ], ""Actions"": [ ""HTO"", ""ISO"" ] }" 它会在每个像这样的单元格中显示:“ {”“ BehaviourEntryId”“:13798177,”“ LearnerId”“:245277,”“ LearnerCode”“:”“ 2009-0080”“,”“ RegGroup”“:”“ U6- RWE“”,“” Behaviour“”:“” Negative“”,“ IncidentDate”“:”“ 2017-02-07”“,”“ Subject”“:”“ BE”“,”“ Location”“: “” CLS“”,“”已发布“”:“”是“”,“创建者”“:”“ DhDr”“,”“编辑者”“:null,”“代理人”“:”“ DiRo”“, “”状态“”:“”已完成“”,“”详细信息“”:[{“类别”“:”“ CL”“,”类型“”:“”类别“”,“严重性”“: “” S2“”,“” point“”:0},{“”类别“”:“” CL“”,“”类型“”:“” CLBEH“”,“”严重性“”:“” S2“ “,”“ point”“:2}],”“ Comments”“:[{”“ BehaviourEntryCommentId”“:5648278,”“机密”“:true,”“ Comment”“:”“要求上厕所并走到了最远的那个,只是为了浪费时间。“”},{“” BehaviourEntryCommentId“”:5648279,“”机密“”:假,“” Comment“”:“”地板上的口香糖“”}, {“” BehaviourEntryCommentId“”:5648280,“”机密“”:false,“” Comment“”:“”对工作人员无礼“”}],“”操作“”:[“” HTO“”,“ “ ISO”“]}”

How do I separate the text to how I want in the cell range and bulk update it? 如何在单元格区域中将文本分隔为所需的文本并批量更新?

If you mean something like two columns with one row being "BehaviourEntryId" and the other row being 13798177, you can try something like this: 如果您的意思是类似两列,其中一行为“ BehaviourEntryId”,另一行为13798177,则可以尝试如下操作:

import json
response = json.loads(response_body) #decode the json response string, returns a dict
response_pairs = list(response.items)

for i in range(1, len(response_body)+1):
    current_pair = response_pairs[i-1]
    current_key =  current_pair[0]
    current_value = current_pair[1]
    wks.update_acell('A{}'.format(i), current_key)
    wks.update_acell('B{}'.format(i), current_value)

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

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