类型错误:字符串索引必须是整数,而使用 Gerrit rest API

当我尝试通过 gerrit rest API 获取数据时,我收到了上面提到的错误。代码如下:


from requests.auth import HTTPBasicAuth 

from pygerrit2.rest import GerritRestAPI 

import os 

auth = HTTPBasicAuth('user', 'pass') headers={'Content-Type': 'application/json; charset=UTF-8'} 

rest = GerritRestAPI(url='testgerriturl.com', auth = auth) 

changes = rest.get("changes/?q=all&o=DETAILED_ACCOUNTS&n=5", headers={'Content-Type': 'application/json'}) 

data={'status':[]}


for change in changes:

       for d in change:

            data['status'].append(d[u'status'])

我正在使用的 json 类型为:


{ u 'status': u 'MERGED', u 'topic': u 'master_e', u 'updated': u '2018-09-04 05:40:19.000000000', u 'insertions': 549, u 'created ': u '2017-08-31 08:03:05.000000000', u 'change_id': u 'I7ed2f8da71e38f4908acf92f36dd', u 'hashtags': [], 'u:'18submitted' '18deletions 09-04 05:39:48.000000000', u 'submittable': False, u 'project': u 'testdemo', u 'branch': u 'master', u 'owner': { u 'username': u ' usertest', u 'email': u 'user@abc.com', u 'name': u 'userdemo', u '_account_id': 36710 }, u '_number': 71090131, u 'id': u 'fm~master~I7ed71f4908acf92f36ddac3ff85b271e', u 'subject': u '%TBC FC: OSS_FC_0 App_NBI: RESTful Web Service Data Access API-PM Support : add IUM scripts' }


我希望以以下形式输出:


{'status': ['MERGED'], 'deletions': [178], 'updated': ['2018-09-03 20:58:26.000000000'], 'insertions': [664], 'created': [2018-08-28 08:16:56.000000000']}

我在这里做错了,有人可以纠正我这里需要做什么吗?谢谢


达令说
浏览 260回答 1
1回答

慕尼黑的夜晚无繁华

这应该有帮助。changes = { u'status': u'MERGED', u'topic': u'master_e', u'updated': u'2018-09-04 05:40:19.000000000', u'insertions': 549, u'created': u'2017-08-31 08:03:05.000000000', u'change_id': u'I7ed2f8da71e38f4908acf92f36dd', u'hashtags': [], u'deletions': 178, u'submitted': u'2018-09-04 05:39:48.000000000', u'submittable': False, u'project': u'testdemo', u'branch': u'master', u'owner': { u'username': u'usertest', u'email': u'user@abc.com', u'name': u'userdemo', u'_account_id': 36710 }, u'_number': 71090131, u'id': u'fm~master~I7ed71f4908acf92f36ddac3ff85b271e', u'subject': u'%TBC FC: OSS_FC_0 App_NBI: RESTful Web Service Data Access API- PM Support : add IUM scripts' }data={'status':[], 'deletions':[], 'updated':[], 'insertions':[], 'created':[], }for k, v in data.items():    if k in changes:        data[k].append(changes[k])print(data)输出:{'status': [u'MERGED'], 'deletions': [178], 'updated': [u'2018-09-04 05:40:19.000000000'], 'insertions': [549], 'created': [u'2017-08-31 08:03:05.000000000']}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python