`TypeError: string indices must be integers`

`TypeError: string indices must be integers`

我正在尝试列出存储库中拉取请求的标题和编号。我想 return JSON 作为字典并打印拉取请求的标题和编号。

如果我只单独打印标题或编号,我会得到预期的输出,但如果组合这些值进行打印,我会得到 TypeError: string indices must be integers

#!/usr/bin/env python
import github3
from github3 import login, GitHub
import requests
import json
import sys

auth = dict(username="xxxxxxxxx",token="xxxxxxxxx")
gh = login(**auth)

result = gh.repository(owner="xxx", repository="xxxx").pull_request(x)
data  = result.as_dict()
print data['title']['number']

的确,二位炼金术士说的是真的。给出这个例子:

>>> auth = dict(username='larsks', token='mytoken')
>>> gh = login(**auth)
>>> result = gh.repository(owner='ansible', repository='ansible').pull_request(12165)
>>> data = result.as_dict()

我们可以看到data['title']是一个字符串:

>>> data['title']
'Be systematic about parsing and validating hostnames and addresses (proof of concept)'

如果我们想要PR号码,我们可以要求:

>>> data['number']
12165