python gspread从响应体中更新多个细胞

时间:2017-10-14 20:35:57

标签: python api gspread

我正在使用此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)

我知道cell.value =响应的身体是错的,我不知道我怎么能把它弄好 - 我被卡住了。

它出现在每个单元格中: " {     "" BehaviourEntryId"":13798177,     "" LearnerId"":245277,     "" LearnerCode"":"" 2009-0080"",     "" RegGroup"":"" U6-RWE"",     ""行为"":""否定"",     "" IncidentDate"":"" 2017-02-07"",     ""主题"":"" BE"",     ""位置"":"" CLS"",     ""发布"":""是"",     ""创作者"":"" DhDr"",     ""编辑"":null,     ""受让人"":"" DiRo"",     ""状态"":""已完成"",     ""细节"":[                   {                     ""类别"":"" CL"",                     ""键入"":"" CLatt"",                     ""严重性"":"" S2"",                     ""指向"":0                   },                   {                     ""类别"":"" CL"",                     ""输入"":"" CLBEH"",                     ""严重性"":"" S2"",                     ""指向"":2                   }                 ]     ""评论"":[                   {                     "" BehaviourEntryCommentId"":5648278,                     ""机密"":是的,                     ""评论"":""要求上厕所去最远的地方只是为了浪费时间。"&#34 ;                   },                   {                     "" BehaviourEntryCommentId"":5648279,                     ""机密"":false,                     ""评论"":""在地板上吐口水""                   },                   {                     "" BehaviourEntryCommentId"":5648280,                     ""机密"":false,                     ""评论"":""对工作人员的态度很粗鲁""                   }                 ]     ""行动"":[                   "" HTO"&#34 ;,                   "" ISO""                 ] }"

如何将文本分隔到我想要的单元格范围并批量更新呢?

1 个答案:

答案 0 :(得分:0)

如果您的意思是两列,其中一行是“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)