2017-10-14 49 views
0

我使用这个python脚本采取从浦API响应: http://docs.progresso.apiary.io/#reference/behaviour/behaviour-events-collection/get-behaviour-events蟒蛇gspread从人体效应初探更新多个细胞

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”“, ”“Behavior”“:”“Negative”“, ”“IncidentDate”“:”“2017-02-07”“, ”“Subject”“: “”“”“”“”位置“”:“”CLS“”, “ “:null, ”“Assignee”“:”“DiRo”“, ”“Status”“:”“Completed”“, ”“详细信息“”:“类别”“:”“CLatt”“,” “”严重性“”:“”S2“”, “”点“ “:0 }, { ”“ 类别”, “: ”“ CL””, “” 类型 “”: “” CLBEH “”, “” 严重性 “”: “” S2 “”, “”点 “”:2 } ], “” 意见 “”:[ { “” BehaviourEntryCommentId “”:5648278, “” 机密 “”:真实, “” 评论 “”: “” 问去去了厕所,去了最远的一个地方,为了浪费时间。输入法 “” }, { “” BehaviourEntryCommentId “”:5648279, “” 机密 “”:假的, “” 评论 “”: “地板上” 稚贝胶去 “” }, { “ “BehaviourEntryCommentId” “:5648280, ”“ 机密” “:假的, ”“ 评论” “: ”“ 很粗鲁员工的memeber”” } ], “” 操作 “”: “” HTO “”, “”ISO“” “ }”

如何将文本与单元格范围内的文本分开并批量更新?

回答

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) 
+0

哪来这种添加到代码的最佳地点?我加了这个,并且一直给出无效的语法错误。 –

+0

我不太确定不知道整个代码。语法错误在哪里?导入应该始终放在代码的顶部。 – tehtea