-2

我正在尝试在存储库中列出拉取请求的标题和编号。我想将 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']
4

1 回答 1

2

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

>>> 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
于 2015-08-31T19:06:43.820 回答